8989import java .util .function .BiConsumer ;
9090import java .util .function .Function ;
9191
92+ import static org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .parseIndexOptions ;
9293import static org .elasticsearch .inference .TaskType .SPARSE_EMBEDDING ;
9394import static org .elasticsearch .inference .TaskType .TEXT_EMBEDDING ;
9495import 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