4848import org .elasticsearch .xcontent .DeprecationHandler ;
4949import org .elasticsearch .xcontent .NamedXContentRegistry ;
5050import org .elasticsearch .xcontent .ParseField ;
51- import org .elasticsearch .xcontent .ToXContent ;
5251import org .elasticsearch .xcontent .XContentBuilder ;
5352import org .elasticsearch .xcontent .XContentParser ;
5453import org .elasticsearch .xcontent .XContentParser .Token ;
@@ -98,7 +97,7 @@ public static class Builder extends FieldMapper.Builder {
9897
9998 private final Parameter <Boolean > stored = Parameter .storeParam (m -> toType (m ).fieldType ().isStored (), false );
10099 private final Parameter <Map <String , String >> meta = Parameter .metaParam ();
101- private final Parameter <IndexOptions > indexOptions = new Parameter <>(
100+ private final Parameter <SparseVectorIndexOptions > indexOptions = new Parameter <>(
102101 SPARSE_VECTOR_INDEX_OPTIONS ,
103102 true ,
104103 () -> null ,
@@ -128,9 +127,9 @@ protected Parameter<?>[] getParameters() {
128127
129128 @ Override
130129 public SparseVectorFieldMapper build (MapperBuilderContext context ) {
131- IndexOptions builderIndexOptions = indexOptions .getValue ();
130+ SparseVectorIndexOptions builderIndexOptions = indexOptions .getValue ();
132131 if (builderIndexOptions == null ) {
133- builderIndexOptions = getDefaultIndexOptions (indexVersionCreated );
132+ builderIndexOptions = SparseVectorIndexOptions . getDefaultIndexOptions (indexVersionCreated );
134133 }
135134
136135 final boolean syntheticVectorFinal = context .isSourceSynthetic () == false && isSyntheticVector ;
@@ -149,33 +148,34 @@ public SparseVectorFieldMapper build(MapperBuilderContext context) {
149148 );
150149 }
151150
152- private IndexOptions getDefaultIndexOptions (IndexVersion indexVersion ) {
153- return (indexVersion .onOrAfter (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION )
154- || indexVersion .between (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION_8_X , IndexVersions .UPGRADE_TO_LUCENE_10_0_0 ))
155- ? IndexOptions .DEFAULT_PRUNING_INDEX_OPTIONS
156- : null ;
151+ private boolean indexOptionsSerializerCheck (boolean includeDefaults , boolean isConfigured , SparseVectorIndexOptions value ) {
152+ return includeDefaults || (SparseVectorIndexOptions .isDefaultOptions (value , indexVersionCreated ) == false );
157153 }
158154
159- private boolean indexOptionsSerializerCheck ( boolean includeDefaults , boolean isConfigured , IndexOptions value ) {
160- return includeDefaults || ( IndexOptions . isDefaultOptions ( value , indexVersionCreated ) == false );
155+ public void setIndexOptions ( SparseVectorIndexOptions sparseVectorIndexOptions ) {
156+ indexOptions . setValue ( sparseVectorIndexOptions );
161157 }
162158 }
163159
164- public IndexOptions getIndexOptions () {
160+ public SparseVectorIndexOptions getIndexOptions () {
165161 return fieldType ().getIndexOptions ();
166162 }
167163
168- private static final ConstructingObjectParser <IndexOptions , Void > INDEX_OPTIONS_PARSER = new ConstructingObjectParser <>(
164+ private static final ConstructingObjectParser <SparseVectorIndexOptions , Void > INDEX_OPTIONS_PARSER = new ConstructingObjectParser <>(
169165 SPARSE_VECTOR_INDEX_OPTIONS ,
170- args -> new IndexOptions ((Boolean ) args [0 ], (TokenPruningConfig ) args [1 ])
166+ args -> new SparseVectorIndexOptions ((Boolean ) args [0 ], (TokenPruningConfig ) args [1 ])
171167 );
172168
173169 static {
174- INDEX_OPTIONS_PARSER .declareBoolean (optionalConstructorArg (), IndexOptions .PRUNE_FIELD_NAME );
175- INDEX_OPTIONS_PARSER .declareObject (optionalConstructorArg (), TokenPruningConfig .PARSER , IndexOptions .PRUNING_CONFIG_FIELD_NAME );
170+ INDEX_OPTIONS_PARSER .declareBoolean (optionalConstructorArg (), SparseVectorIndexOptions .PRUNE_FIELD_NAME );
171+ INDEX_OPTIONS_PARSER .declareObject (
172+ optionalConstructorArg (),
173+ TokenPruningConfig .PARSER ,
174+ SparseVectorIndexOptions .PRUNING_CONFIG_FIELD_NAME
175+ );
176176 }
177177
178- private static SparseVectorFieldMapper . IndexOptions parseIndexOptions (MappingParserContext context , Object propNode ) {
178+ private static SparseVectorIndexOptions parseIndexOptions (MappingParserContext context , Object propNode ) {
179179 if (propNode == null ) {
180180 return null ;
181181 }
@@ -212,7 +212,7 @@ private static SparseVectorFieldMapper.IndexOptions parseIndexOptions(MappingPar
212212
213213 public static final class SparseVectorFieldType extends MappedFieldType {
214214 private final IndexVersion indexVersionCreated ;
215- private final IndexOptions indexOptions ;
215+ private final SparseVectorIndexOptions indexOptions ;
216216
217217 public SparseVectorFieldType (IndexVersion indexVersionCreated , String name , boolean isStored , Map <String , String > meta ) {
218218 this (indexVersionCreated , name , isStored , meta , null );
@@ -223,14 +223,14 @@ public SparseVectorFieldType(
223223 String name ,
224224 boolean isStored ,
225225 Map <String , String > meta ,
226- @ Nullable SparseVectorFieldMapper . IndexOptions indexOptions
226+ @ Nullable SparseVectorIndexOptions indexOptions
227227 ) {
228228 super (name , true , isStored , false , TextSearchInfo .SIMPLE_MATCH_ONLY , meta );
229229 this .indexVersionCreated = indexVersionCreated ;
230230 this .indexOptions = indexOptions ;
231231 }
232232
233- public IndexOptions getIndexOptions () {
233+ public SparseVectorIndexOptions getIndexOptions () {
234234 return indexOptions ;
235235 }
236236
@@ -560,15 +560,18 @@ public void reset() {
560560 }
561561 }
562562
563- public static class IndexOptions implements ToXContent {
563+ public static class SparseVectorIndexOptions implements IndexOptions {
564564 public static final ParseField PRUNE_FIELD_NAME = new ParseField ("prune" );
565565 public static final ParseField PRUNING_CONFIG_FIELD_NAME = new ParseField ("pruning_config" );
566- public static final IndexOptions DEFAULT_PRUNING_INDEX_OPTIONS = new IndexOptions (true , new TokenPruningConfig ());
566+ public static final SparseVectorIndexOptions DEFAULT_PRUNING_INDEX_OPTIONS = new SparseVectorIndexOptions (
567+ true ,
568+ new TokenPruningConfig ()
569+ );
567570
568571 final Boolean prune ;
569572 final TokenPruningConfig pruningConfig ;
570573
571- IndexOptions (@ Nullable Boolean prune , @ Nullable TokenPruningConfig pruningConfig ) {
574+ public SparseVectorIndexOptions (@ Nullable Boolean prune , @ Nullable TokenPruningConfig pruningConfig ) {
572575 if (pruningConfig != null && (prune == null || prune == false )) {
573576 throw new IllegalArgumentException (
574577 "["
@@ -585,14 +588,37 @@ public static class IndexOptions implements ToXContent {
585588 this .pruningConfig = pruningConfig ;
586589 }
587590
588- public static boolean isDefaultOptions (IndexOptions indexOptions , IndexVersion indexVersion ) {
589- IndexOptions defaultIndexOptions = indexVersionSupportsDefaultPruningConfig (indexVersion )
591+ public static boolean isDefaultOptions (SparseVectorIndexOptions indexOptions , IndexVersion indexVersion ) {
592+ SparseVectorIndexOptions defaultIndexOptions = indexVersionSupportsDefaultPruningConfig (indexVersion )
590593 ? DEFAULT_PRUNING_INDEX_OPTIONS
591594 : null ;
592595
593596 return Objects .equals (indexOptions , defaultIndexOptions );
594597 }
595598
599+ public static SparseVectorIndexOptions getDefaultIndexOptions (IndexVersion indexVersion ) {
600+ return indexVersionSupportsDefaultPruningConfig (indexVersion ) ? DEFAULT_PRUNING_INDEX_OPTIONS : null ;
601+ }
602+
603+ public static SparseVectorIndexOptions parseFromMap (Map <String , Object > map ) {
604+ if (map == null ) {
605+ return null ;
606+ }
607+
608+ try {
609+ XContentParser parser = new MapXContentParser (
610+ NamedXContentRegistry .EMPTY ,
611+ DeprecationHandler .IGNORE_DEPRECATIONS ,
612+ map ,
613+ XContentType .JSON
614+ );
615+
616+ return INDEX_OPTIONS_PARSER .parse (parser , null );
617+ } catch (IOException ioEx ) {
618+ throw new UncheckedIOException (ioEx );
619+ }
620+ }
621+
596622 public Boolean getPrune () {
597623 return prune ;
598624 }
@@ -626,7 +652,7 @@ public final boolean equals(Object other) {
626652 return false ;
627653 }
628654
629- IndexOptions otherAsIndexOptions = (IndexOptions ) other ;
655+ SparseVectorIndexOptions otherAsIndexOptions = (SparseVectorIndexOptions ) other ;
630656 return Objects .equals (prune , otherAsIndexOptions .prune ) && Objects .equals (pruningConfig , otherAsIndexOptions .pruningConfig );
631657 }
632658
0 commit comments