|
52 | 52 | import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.ElementType; |
53 | 53 | import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.VectorSimilarity; |
54 | 54 | import org.elasticsearch.index.query.SearchExecutionContext; |
| 55 | +import org.elasticsearch.inference.SimilarityMeasure; |
55 | 56 | import org.elasticsearch.search.lookup.Source; |
56 | 57 | import org.elasticsearch.search.lookup.SourceProvider; |
57 | 58 | import org.elasticsearch.search.vectors.VectorData; |
@@ -1337,6 +1338,70 @@ public void testDefaultParamsIndexByDefault() throws Exception { |
1337 | 1338 | assertEquals(VectorSimilarity.COSINE, denseVectorFieldType.getSimilarity()); |
1338 | 1339 | } |
1339 | 1340 |
|
| 1341 | + public void testDefaultIndexOptions() throws IOException { |
| 1342 | + for (int i = 0; i < 100; i++) { |
| 1343 | + // Pick a random index version from one of three eras that each produce different default index options |
| 1344 | + int era = randomIntBetween(0, 3); |
| 1345 | + IndexVersion indexVersion = switch (era) { |
| 1346 | + case 0 -> IndexVersionUtils.randomVersionBetween( |
| 1347 | + random(), |
| 1348 | + IndexVersionUtils.getLowestReadCompatibleVersion(), |
| 1349 | + IndexVersionUtils.getPreviousVersion(DenseVectorFieldMapper.DEFAULT_TO_INT8) |
| 1350 | + ); |
| 1351 | + case 1 -> IndexVersionUtils.randomVersionBetween( |
| 1352 | + random(), |
| 1353 | + DenseVectorFieldMapper.DEFAULT_TO_INT8, |
| 1354 | + IndexVersionUtils.getPreviousVersion(DenseVectorFieldMapper.DEFAULT_TO_BBQ) |
| 1355 | + ); |
| 1356 | + case 2 -> IndexVersionUtils.randomVersionBetween( |
| 1357 | + random(), |
| 1358 | + DenseVectorFieldMapper.DEFAULT_TO_BBQ, |
| 1359 | + IndexVersionUtils.getPreviousVersion(DenseVectorFieldMapper.BFLOAT16_DEFAULT_INDEX_OPTIONS_BACKPORT) |
| 1360 | + ); |
| 1361 | + case 3 -> IndexVersionUtils.randomVersionBetween( |
| 1362 | + random(), |
| 1363 | + DenseVectorFieldMapper.BFLOAT16_DEFAULT_INDEX_OPTIONS_BACKPORT, |
| 1364 | + IndexVersion.current() |
| 1365 | + ); |
| 1366 | + default -> throw new AssertionError("Unexpected value: " + era); |
| 1367 | + }; |
| 1368 | + |
| 1369 | + boolean defaultInt8Hnsw = indexVersion.onOrAfter(DenseVectorFieldMapper.DEFAULT_TO_INT8); |
| 1370 | + boolean defaultBBQHnsw = indexVersion.onOrAfter(DenseVectorFieldMapper.DEFAULT_TO_BBQ); |
| 1371 | + |
| 1372 | + final ElementType elementType = randomFrom(ElementType.values()); |
| 1373 | + final int dims = DenseVectorFieldMapperTestUtils.randomCompatibleDimensions(elementType, 512); |
| 1374 | + final VectorSimilarity similarity = randomFrom( |
| 1375 | + DenseVectorFieldMapperTestUtils.getSupportedSimilarities(elementType) |
| 1376 | + .stream() |
| 1377 | + .map(SimilarityMeasure::vectorSimilarity) |
| 1378 | + .toList() |
| 1379 | + ); |
| 1380 | + |
| 1381 | + MapperService mapperService = createMapperService(indexVersion, fieldMapping(b -> { |
| 1382 | + b.field("type", "dense_vector"); |
| 1383 | + b.field("index", true); |
| 1384 | + b.field("element_type", elementType.toString()); |
| 1385 | + b.field("dims", dims); |
| 1386 | + b.field("similarity", similarity.toString()); |
| 1387 | + })); |
| 1388 | + |
| 1389 | + DenseVectorFieldMapper mapper = (DenseVectorFieldMapper) mapperService.mappingLookup().getMapper("field"); |
| 1390 | + DenseVectorFieldMapper.DenseVectorIndexOptions indexOptions = mapper.fieldType().getIndexOptions(); |
| 1391 | + |
| 1392 | + if (DenseVectorFieldMapperTestUtils.elementTypesWithDefaultIndexOptions(indexVersion).contains(elementType) == false) { |
| 1393 | + assertNull(indexOptions); |
| 1394 | + } else if (defaultBBQHnsw && dims >= DenseVectorFieldMapper.BBQ_DIMS_DEFAULT_THRESHOLD) { |
| 1395 | + assertThat(indexOptions, instanceOf(DenseVectorFieldMapper.BBQHnswIndexOptions.class)); |
| 1396 | + } else if (defaultInt8Hnsw) { |
| 1397 | + // INT8 era, or BBQ era with dims below the BBQ threshold |
| 1398 | + assertThat(indexOptions, instanceOf(DenseVectorFieldMapper.Int8HnswIndexOptions.class)); |
| 1399 | + } else { |
| 1400 | + assertNull(indexOptions); |
| 1401 | + } |
| 1402 | + } |
| 1403 | + } |
| 1404 | + |
1340 | 1405 | public void testValidateOnBuild() { |
1341 | 1406 | final MapperBuilderContext context = MapperBuilderContext.root(false, false); |
1342 | 1407 |
|
|
0 commit comments