Skip to content

Commit 5023cdd

Browse files
committed
semantic_text add index_options
1 parent a1b16d9 commit 5023cdd

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ public Builder elementType(ElementType elementType) {
279279
return this;
280280
}
281281

282+
public Builder indexOptions(IndexOptions indexOptions) {
283+
this.indexOptions.setValue(indexOptions);
284+
return this;
285+
}
286+
282287
@Override
283288
public DenseVectorFieldMapper build(MapperBuilderContext context) {
284289
// Validate again here because the dimensions or element type could have been set programmatically,
@@ -1177,7 +1182,7 @@ public final String toString() {
11771182
public abstract VectorSimilarityFunction vectorSimilarityFunction(IndexVersion indexVersion, ElementType elementType);
11781183
}
11791184

1180-
abstract static class IndexOptions implements ToXContent {
1185+
public abstract static class IndexOptions implements ToXContent {
11811186
final VectorIndexType type;
11821187

11831188
IndexOptions(VectorIndexType type) {
@@ -2319,7 +2324,7 @@ public FieldMapper.Builder getMergeBuilder() {
23192324
return new Builder(leafName(), indexCreatedVersion).init(this);
23202325
}
23212326

2322-
private static IndexOptions parseIndexOptions(String fieldName, Object propNode) {
2327+
public static IndexOptions parseIndexOptions(String fieldName, Object propNode) {
23232328
@SuppressWarnings("unchecked")
23242329
Map<String, ?> indexOptionsMap = (Map<String, ?>) propNode;
23252330
Object typeNode = indexOptionsMap.remove("type");

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import java.util.function.BiConsumer;
9090
import java.util.function.Function;
9191

92+
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.parseIndexOptions;
9293
import static org.elasticsearch.inference.TaskType.SPARSE_EMBEDDING;
9394
import static org.elasticsearch.inference.TaskType.TEXT_EMBEDDING;
9495
import static org.elasticsearch.search.SearchService.DEFAULT_SIZE;
@@ -136,6 +137,10 @@ public static BiConsumer<String, MappingParserContext> validateParserContext(Str
136137
};
137138
}
138139

140+
private static Builder builder(FieldMapper in) {
141+
return ((SemanticTextFieldMapper) in).builder;
142+
}
143+
139144
public static class Builder extends FieldMapper.Builder {
140145
private final boolean useLegacyFormat;
141146

@@ -175,6 +180,20 @@ public static class Builder extends FieldMapper.Builder {
175180
Objects::toString
176181
).acceptsNull().setMergeValidator(SemanticTextFieldMapper::canMergeModelSettings);
177182

183+
private final Parameter<DenseVectorFieldMapper.IndexOptions> indexOptions = new Parameter<>(
184+
"index_options",
185+
true,
186+
() -> null,
187+
(n, c, o) -> o == null ? null : parseIndexOptions(n, o),
188+
m -> builder(m).indexOptions.get(),
189+
(b, n, v) -> {
190+
if (v != null) {
191+
b.field(n, v);
192+
}
193+
},
194+
Objects::toString
195+
);
196+
178197
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
179198

180199
private Function<MapperBuilderContext, ObjectMapper> inferenceFieldBuilder;
@@ -197,6 +216,7 @@ public Builder(String name, Function<Query, BitSetProducer> bitSetProducer, Inde
197216
indexSettings.getIndexVersionCreated(),
198217
useLegacyFormat,
199218
modelSettings.get(),
219+
indexOptions.get(),
200220
bitSetProducer,
201221
indexSettings
202222
);
@@ -265,7 +285,8 @@ public SemanticTextFieldMapper build(MapperBuilderContext context) {
265285
useLegacyFormat,
266286
meta.getValue()
267287
),
268-
builderParams(this, context)
288+
builderParams(this, context),
289+
this
269290
);
270291
}
271292

@@ -306,9 +327,12 @@ private SemanticTextFieldMapper copySettings(SemanticTextFieldMapper mapper, Map
306327
}
307328
}
308329

309-
private SemanticTextFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams builderParams) {
330+
private final Builder builder;
331+
332+
private SemanticTextFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams builderParams, Builder builder) {
310333
super(simpleName, mappedFieldType, builderParams);
311334
ensureMultiFields(builderParams.multiFields().iterator());
335+
this.builder = builder;
312336
}
313337

314338
private void ensureMultiFields(Iterator<FieldMapper> mappers) {
@@ -910,18 +934,20 @@ private static ObjectMapper createInferenceField(
910934
IndexVersion indexVersionCreated,
911935
boolean useLegacyFormat,
912936
@Nullable MinimalServiceSettings modelSettings,
937+
@Nullable DenseVectorFieldMapper.IndexOptions indexOptions,
913938
Function<Query, BitSetProducer> bitSetProducer,
914939
IndexSettings indexSettings
915940
) {
916941
return new ObjectMapper.Builder(INFERENCE_FIELD, Optional.of(ObjectMapper.Subobjects.ENABLED)).dynamic(ObjectMapper.Dynamic.FALSE)
917-
.add(createChunksField(indexVersionCreated, useLegacyFormat, modelSettings, bitSetProducer, indexSettings))
942+
.add(createChunksField(indexVersionCreated, useLegacyFormat, modelSettings, indexOptions, bitSetProducer, indexSettings))
918943
.build(context);
919944
}
920945

921946
private static NestedObjectMapper.Builder createChunksField(
922947
IndexVersion indexVersionCreated,
923948
boolean useLegacyFormat,
924949
@Nullable MinimalServiceSettings modelSettings,
950+
@Nullable DenseVectorFieldMapper.IndexOptions indexOptions,
925951
Function<Query, BitSetProducer> bitSetProducer,
926952
IndexSettings indexSettings
927953
) {
@@ -933,7 +959,7 @@ private static NestedObjectMapper.Builder createChunksField(
933959
);
934960
chunksField.dynamic(ObjectMapper.Dynamic.FALSE);
935961
if (modelSettings != null) {
936-
chunksField.add(createEmbeddingsField(indexSettings.getIndexVersionCreated(), modelSettings, useLegacyFormat));
962+
chunksField.add(createEmbeddingsField(indexSettings.getIndexVersionCreated(), modelSettings, indexOptions, useLegacyFormat));
937963
}
938964
if (useLegacyFormat) {
939965
var chunkTextField = new KeywordFieldMapper.Builder(TEXT_FIELD, indexVersionCreated).indexed(false).docValues(false);
@@ -947,6 +973,7 @@ private static NestedObjectMapper.Builder createChunksField(
947973
private static Mapper.Builder createEmbeddingsField(
948974
IndexVersion indexVersionCreated,
949975
MinimalServiceSettings modelSettings,
976+
DenseVectorFieldMapper.IndexOptions indexOptions,
950977
boolean useLegacyFormat
951978
) {
952979
return switch (modelSettings.taskType()) {
@@ -970,6 +997,7 @@ private static Mapper.Builder createEmbeddingsField(
970997
}
971998
denseVectorMapperBuilder.dimensions(modelSettings.dimensions());
972999
denseVectorMapperBuilder.elementType(modelSettings.elementType());
1000+
denseVectorMapperBuilder.indexOptions(indexOptions);
9731001

9741002
yield denseVectorMapperBuilder;
9751003
}

0 commit comments

Comments
 (0)