3636import org .elasticsearch .common .lucene .search .Queries ;
3737import org .elasticsearch .common .settings .Settings ;
3838import org .elasticsearch .core .CheckedConsumer ;
39- import org .elasticsearch .core .Tuple ;
4039import org .elasticsearch .index .IndexVersion ;
4140import org .elasticsearch .index .IndexVersions ;
4241import org .elasticsearch .index .mapper .DocumentMapper ;
@@ -643,56 +642,6 @@ private void addSparseVectorModelSettingsToBuilder(XContentBuilder b) throws IOE
643642 b .endObject ();
644643 }
645644
646- private void setSparseVectorIndexOptionInMapper (XContentBuilder b , SparseVectorFieldMapper .SparseVectorIndexOptions indexOptions )
647- throws IOException {
648- setSparseVectorIndexOptionInMapper (b , indexOptions , null );
649- }
650-
651- private void setSparseVectorIndexOptionInMapper (
652- XContentBuilder b ,
653- SparseVectorFieldMapper .SparseVectorIndexOptions indexOptions ,
654- Tuple <String , Object > injectExtraField
655- ) throws IOException {
656- if (indexOptions == null ) {
657- return ;
658- }
659-
660- b .startObject (INDEX_OPTIONS_FIELD );
661- {
662- b .startObject (SparseVectorFieldMapper .CONTENT_TYPE );
663- {
664- if (indexOptions .getPrune () != null ) {
665- b .field (SparseVectorFieldMapper .SparseVectorIndexOptions .PRUNE_FIELD_NAME .getPreferredName (), indexOptions .getPrune ());
666- }
667-
668- if (indexOptions .getPruningConfig () != null ) {
669- b .startObject (SparseVectorFieldMapper .SparseVectorIndexOptions .PRUNING_CONFIG_FIELD_NAME .getPreferredName ());
670- {
671- b .field (
672- TokenPruningConfig .TOKENS_FREQ_RATIO_THRESHOLD .getPreferredName (),
673- indexOptions .getPruningConfig ().getTokensFreqRatioThreshold ()
674- );
675- b .field (
676- TokenPruningConfig .TOKENS_WEIGHT_THRESHOLD .getPreferredName (),
677- indexOptions .getPruningConfig ().getTokensWeightThreshold ()
678- );
679- b .field (
680- TokenPruningConfig .ONLY_SCORE_PRUNED_TOKENS_FIELD .getPreferredName (),
681- indexOptions .getPruningConfig ().isOnlyScorePrunedTokens ()
682- );
683- }
684- b .endObject ();
685- }
686-
687- if (injectExtraField != null ) {
688- b .field (injectExtraField .v1 (), injectExtraField .v2 ());
689- }
690- }
691- b .endObject ();
692- }
693-
694- }
695-
696645 public void testSparseVectorIndexOptionsValidationAndMapping () throws IOException {
697646 for (int depth = 1 ; depth < 5 ; depth ++) {
698647 SparseVectorFieldMapper .SparseVectorIndexOptions indexOptions = SparseVectorFieldTypeTests .randomSparseVectorIndexOptions ();
@@ -706,8 +655,14 @@ public void testSparseVectorIndexOptionsValidationAndMapping() throws IOExceptio
706655 b .field ("type" , SemanticTextFieldMapper .CONTENT_TYPE );
707656 b .field (INFERENCE_ID_FIELD , inferenceId );
708657 addSparseVectorModelSettingsToBuilder (b );
709- setSparseVectorIndexOptionInMapper (b , indexOptions );
710- b .endObject ();
658+ if (indexOptions != null ) {
659+ b .startObject (INDEX_OPTIONS_FIELD );
660+ {
661+ b .field (SparseVectorFieldMapper .CONTENT_TYPE );
662+ indexOptions .toXContent (b , null );
663+ }
664+ b .endObject ();
665+ }
711666 }
712667 b .endObject ();
713668 }));
@@ -717,7 +672,9 @@ public void testSparseVectorIndexOptionsValidationAndMapping() throws IOExceptio
717672 fieldName ,
718673 true ,
719674 null ,
720- new SemanticTextIndexOptions (SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR , indexOptions )
675+ indexOptions == null
676+ ? null
677+ : new SemanticTextIndexOptions (SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR , indexOptions )
721678 );
722679 }
723680 }
@@ -770,26 +727,6 @@ public void testSparseVectorMappingUpdate() throws IOException {
770727 }
771728 }
772729
773- public void testSparseVectorValidationWithUnknownParameter () throws IOException {
774- for (int depth = 1 ; depth < 5 ; depth ++) {
775- SparseVectorFieldMapper .SparseVectorIndexOptions indexOptions = SparseVectorFieldTypeTests .randomSparseVectorIndexOptions ();
776- String inferenceId = "test_model" ;
777- String fieldName = randomFieldName (depth );
778-
779- Exception exc = expectThrows (MapperParsingException .class , () -> createMapperService (mapping (b -> {
780- b .startObject (fieldName );
781- {
782- b .field ("type" , SemanticTextFieldMapper .CONTENT_TYPE );
783- b .field (INFERENCE_ID_FIELD , inferenceId );
784- setSparseVectorIndexOptionInMapper (b , indexOptions , new Tuple <>("unknown_parameter" , "test" ));
785- b .endObject ();
786- }
787- b .endObject ();
788- })));
789- assertTrue (exc .getMessage ().contains ("[index_options] unknown field [unknown_parameter]" ));
790- }
791- }
792-
793730 public void testUpdateSearchInferenceId () throws IOException {
794731 final String inferenceId = "test_inference_id" ;
795732 final String searchInferenceId1 = "test_search_inference_id_1" ;
@@ -1546,7 +1483,7 @@ public void testSparseVectorIndexOptionsDefaultsBeforeSupport() throws IOExcepti
15461483 "field" ,
15471484 true ,
15481485 null ,
1549- defaultSparseVectorIndexOptions ( mapperService . getIndexSettings (). getIndexVersionCreated ())
1486+ null
15501487 );
15511488 }
15521489
@@ -1662,12 +1599,16 @@ public void testSpecifiedDenseVectorIndexOptions() throws IOException {
16621599
16631600 public void testSpecificSparseVectorIndexOptions () throws IOException {
16641601 for (int i = 0 ; i < 10 ; i ++) {
1665- SparseVectorFieldMapper .SparseVectorIndexOptions testIndexOptions = randomSparseVectorIndexOptions ();
1602+ SparseVectorFieldMapper .SparseVectorIndexOptions testIndexOptions = randomSparseVectorIndexOptions (false );
16661603 var mapperService = createMapperService (fieldMapping (b -> {
16671604 b .field ("type" , SemanticTextFieldMapper .CONTENT_TYPE );
16681605 b .field (INFERENCE_ID_FIELD , "test_inference_id" );
16691606 addSparseVectorModelSettingsToBuilder (b );
1670- setSparseVectorIndexOptionInMapper (b , testIndexOptions );
1607+ b .startObject (INDEX_OPTIONS_FIELD );
1608+ {
1609+ b .field (SparseVectorFieldMapper .CONTENT_TYPE );
1610+ testIndexOptions .toXContent (b , null );
1611+ }
16711612 b .endObject ();
16721613 }), useLegacyFormat , IndexVersions .INFERENCE_METADATA_FIELDS_BACKPORT );
16731614
@@ -1741,7 +1682,7 @@ public static SemanticTextIndexOptions randomSemanticTextIndexOptions(TaskType t
17411682 ? null
17421683 : new SemanticTextIndexOptions (
17431684 SemanticTextIndexOptions .SupportedIndexOptions .SPARSE_VECTOR ,
1744- randomSparseVectorIndexOptions ()
1685+ randomSparseVectorIndexOptions (false )
17451686 );
17461687 }
17471688
0 commit comments