Skip to content

Commit 216f8bc

Browse files
committed
Respect index options if set over defaults
1 parent be4136b commit 216f8bc

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,12 +1703,12 @@ public int doHashCode() {
17031703
}
17041704
}
17051705

1706-
static class Int4HnswIndexOptions extends QuantizedIndexOptions {
1706+
public static class Int4HnswIndexOptions extends QuantizedIndexOptions {
17071707
private final int m;
17081708
private final int efConstruction;
17091709
private final float confidenceInterval;
17101710

1711-
Int4HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector) {
1711+
public Int4HnswIndexOptions(int m, int efConstruction, Float confidenceInterval, RescoreVector rescoreVector) {
17121712
super(VectorIndexType.INT4_HNSW, rescoreVector);
17131713
this.m = m;
17141714
this.efConstruction = efConstruction;

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,16 @@ private static Mapper.Builder createEmbeddingsField(
11231123
DenseVectorFieldMapper.DenseVectorIndexOptions denseVectorIndexOptions =
11241124
(DenseVectorFieldMapper.DenseVectorIndexOptions) indexOptions.indexOptions();
11251125
denseVectorMapperBuilder.indexOptions(denseVectorIndexOptions);
1126-
}
1127-
1128-
DenseVectorFieldMapper.DenseVectorIndexOptions defaultIndexOptions = null;
1129-
if (indexVersionCreated.onOrAfter(SEMANTIC_TEXT_DEFAULTS_TO_BBQ)) {
1130-
defaultIndexOptions = defaultSemanticDenseIndexOptions();
1131-
}
1132-
if (defaultIndexOptions != null
1133-
&& defaultIndexOptions.validate(modelSettings.elementType(), modelSettings.dimensions(), false)) {
1134-
denseVectorMapperBuilder.indexOptions(defaultIndexOptions);
1126+
denseVectorIndexOptions.validate(modelSettings.elementType(), modelSettings.dimensions(), true);
1127+
} else {
1128+
DenseVectorFieldMapper.DenseVectorIndexOptions defaultIndexOptions = null;
1129+
if (indexVersionCreated.onOrAfter(SEMANTIC_TEXT_DEFAULTS_TO_BBQ)) {
1130+
defaultIndexOptions = defaultSemanticDenseIndexOptions();
1131+
}
1132+
if (defaultIndexOptions != null
1133+
&& defaultIndexOptions.validate(modelSettings.elementType(), modelSettings.dimensions(), false)) {
1134+
denseVectorMapperBuilder.indexOptions(defaultIndexOptions);
1135+
}
11351136
}
11361137

11371138
yield denseVectorMapperBuilder;

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapperTests.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,14 +1316,40 @@ public void testDefaultIndexOptions() throws IOException {
13161316
b.field("element_type", "float");
13171317
b.endObject();
13181318
}), useLegacyFormat, IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ);
1319+
assertSemanticTextField(
1320+
mapperService,
1321+
"field",
1322+
true,
1323+
null,
1324+
new SemanticTextIndexOptions(SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR, defaultDenseVectorIndexOptions())
1325+
);
1326+
1327+
// If we explicitly set index options, we respect those over the defaults
1328+
mapperService = createMapperService(fieldMapping(b -> {
1329+
b.field("type", "semantic_text");
1330+
b.field("inference_id", "another_inference_id");
1331+
b.startObject("model_settings");
1332+
b.field("task_type", "text_embedding");
1333+
b.field("dimensions", 100);
1334+
b.field("similarity", "cosine");
1335+
b.field("element_type", "float");
1336+
b.endObject();
1337+
b.startObject("index_options");
1338+
b.startObject("dense_vector");
1339+
b.field("type", "int4_hnsw");
1340+
b.field("m", 25);
1341+
b.field("ef_construction", 100);
1342+
b.endObject();
1343+
b.endObject();
1344+
}), useLegacyFormat, IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ);
13191345
assertSemanticTextField(
13201346
mapperService,
13211347
"field",
13221348
true,
13231349
null,
13241350
new SemanticTextIndexOptions(
13251351
SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR,
1326-
SemanticTextFieldMapper.defaultSemanticDenseIndexOptions()
1352+
new DenseVectorFieldMapper.Int4HnswIndexOptions(25, 100, null, null)
13271353
)
13281354
);
13291355

@@ -1348,10 +1374,7 @@ public void testDefaultIndexOptions() throws IOException {
13481374
"field",
13491375
true,
13501376
null,
1351-
new SemanticTextIndexOptions(
1352-
SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR,
1353-
SemanticTextFieldMapper.defaultSemanticDenseIndexOptions()
1354-
)
1377+
new SemanticTextIndexOptions(SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR, defaultDenseVectorIndexOptions())
13551378
);
13561379

13571380
}

0 commit comments

Comments
 (0)