@@ -198,28 +198,11 @@ protected void mappingWithIndexOptionsPruneFalse(XContentBuilder b) throws IOExc
198198 b .endObject ();
199199 }
200200
201- private void mapping (XContentBuilder b , @ Nullable Boolean prune , PruningConfig pruningConfig , Boolean previousVersion )
201+ private void mapping (XContentBuilder b , @ Nullable Boolean prune , PruningConfig pruningConfig )
202202 throws IOException {
203203 b .field ("type" , "sparse_vector" );
204- if (previousVersion == false && prune != null ) {
205- b .startObject ("index_options" );
206- {
207- b .field ("prune" , prune );
208- if (pruningConfig != PruningConfig .NULL ) {
209- b .startObject ("pruning_config" );
210- {
211- if (pruningConfig == PruningConfig .EXPLICIT_DEFAULT ) {
212- b .field ("tokens_freq_ratio_threshold" , TokenPruningConfig .DEFAULT_TOKENS_FREQ_RATIO_THRESHOLD );
213- b .field ("tokens_weight_threshold" , TokenPruningConfig .DEFAULT_TOKENS_WEIGHT_THRESHOLD );
214- } else if (pruningConfig == PruningConfig .STRICT ) {
215- b .field ("tokens_freq_ratio_threshold" , STRICT_TOKENS_FREQ_RATIO_THRESHOLD );
216- b .field ("tokens_weight_threshold" , STRICT_TOKENS_WEIGHT_THRESHOLD );
217- }
218- }
219- b .endObject ();
220- }
221- }
222- b .endObject ();
204+ if (prune != null ) {
205+ b .field ("index_options" , new SparseVectorFieldMapper .SparseVectorIndexOptions (prune , pruningConfig .tokenPruningConfig ));
223206 }
224207 }
225208
@@ -727,11 +710,22 @@ private enum PruningScenario {
727710 STRICT_PRUNING // Stricter pruning with higher thresholds
728711 }
729712
730- private enum PruningConfig {
731- NULL ,
732- EXPLICIT_DEFAULT ,
733- STRICT
734- }
713+
714+ private enum PruningConfig {
715+ NULL (null ),
716+ EXPLICIT_DEFAULT (new TokenPruningConfig ()),
717+ STRICT (new TokenPruningConfig (
718+ STRICT_TOKENS_FREQ_RATIO_THRESHOLD ,
719+ STRICT_TOKENS_WEIGHT_THRESHOLD ,
720+ false
721+ ));
722+
723+ public final @ Nullable TokenPruningConfig tokenPruningConfig ;
724+
725+ PruningConfig (@ Nullable TokenPruningConfig tokenPruningConfig ) {
726+ this .tokenPruningConfig = tokenPruningConfig ;
727+ }
728+ }
735729
736730 private final Set <PruningOptions > validIndexPruningScenarios = Set .of (
737731 new PruningOptions (false , PruningConfig .NULL ),
@@ -785,8 +779,8 @@ public void testPruningScenarios() throws Exception {
785779 }
786780 }
787781
788- private XContentBuilder getIndexMapping (PruningOptions pruningOptions , Boolean usePreviousIndex ) throws IOException {
789- return fieldMapping (b -> mapping (b , pruningOptions .prune () , pruningOptions .pruningConfig (), usePreviousIndex ));
782+ private XContentBuilder getIndexMapping (PruningOptions pruningOptions ) throws IOException {
783+ return fieldMapping (b -> mapping (b , pruningOptions .prune , pruningOptions .pruningConfig ));
790784 }
791785
792786 private void assertQueryContains (List <Query > expectedClauses , Query query ) {
@@ -799,53 +793,33 @@ private void assertQueryContains(List<Query> expectedClauses, Query query) {
799793 assertThat (shouldClauses , Matchers .containsInAnyOrder (expectedClauses .toArray ()));
800794 }
801795
802- private PruningScenario getPruningLevel (PruningConfig config ) {
803- if (config == PruningConfig .STRICT ) {
804- return PruningScenario .STRICT_PRUNING ;
805- }
806- return PruningScenario .DEFAULT_PRUNING ;
807- }
808-
809796 private PruningScenario getEffectivePruningScenario (
810797 PruningOptions indexPruningOptions ,
811798 PruningOptions queryPruningOptions ,
812- Boolean usePreviousIndex
799+ IndexVersion indexVersion
813800 ) {
814- if (usePreviousIndex ) {
815- return (queryPruningOptions .prune != null && queryPruningOptions .prune )
816- ? getPruningLevel (queryPruningOptions .pruningConfig )
817- : PruningScenario .NO_PRUNING ;
818- }
819-
820- Boolean shouldPrune = indexPruningOptions .prune ;
821- if (queryPruningOptions .prune != null ) {
822- shouldPrune = queryPruningOptions .prune ;
801+ Boolean shouldPrune = queryPruningOptions .prune ;
802+ if (shouldPrune == null ) {
803+ shouldPrune = indexPruningOptions .prune ;
823804 }
824805
825- if (shouldPrune != null && shouldPrune == false ) {
826- // Pruning is explicitly disabled
827- return PruningScenario .NO_PRUNING ;
806+ if (shouldPrune == null ) {
807+ shouldPrune = indexVersion .onOrAfter (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT );
828808 }
829809
830- return queryPruningOptions .pruningConfig != PruningConfig .NULL
831- ? getPruningLevel (queryPruningOptions .pruningConfig )
832- : getPruningLevel (indexPruningOptions .pruningConfig );
833- }
834-
835- private Tuple <Boolean , TokenPruningConfig > getQueryPruneConfig (PruningOptions queryPruningOptions ) {
836- Boolean prune = queryPruningOptions .prune ;
837- TokenPruningConfig tokenPruningConfig = null ;
838- if (queryPruningOptions .pruningConfig != PruningConfig .NULL ) {
839- switch (queryPruningOptions .pruningConfig ) {
840- case EXPLICIT_DEFAULT -> tokenPruningConfig = new TokenPruningConfig ();
841- case STRICT -> tokenPruningConfig = new TokenPruningConfig (
842- STRICT_TOKENS_FREQ_RATIO_THRESHOLD ,
843- STRICT_TOKENS_WEIGHT_THRESHOLD ,
844- false
845- );
810+ PruningScenario pruningScenario = PruningScenario .NO_PRUNING ;
811+ if (shouldPrune ) {
812+ PruningConfig pruningConfig = queryPruningOptions .pruningConfig ;
813+ if (pruningConfig == PruningConfig .NULL ) {
814+ pruningConfig = indexPruningOptions .pruningConfig ;
846815 }
816+ pruningScenario = switch (pruningConfig ) {
817+ case STRICT -> PruningScenario .STRICT_PRUNING ;
818+ case EXPLICIT_DEFAULT , NULL -> PruningScenario .DEFAULT_PRUNING ;
819+ };
847820 }
848- return new Tuple <>(prune , tokenPruningConfig );
821+
822+ return pruningScenario ;
849823 }
850824
851825 private List <Query > getExpectedQueryClauses (
@@ -868,19 +842,12 @@ private List<Query> getExpectedQueryClauses(
868842 }
869843
870844 private void assertPruningScenario (PruningOptions indexPruningOptions , PruningOptions queryPruningOptions ) throws IOException {
871-
872- boolean usePreIndexOptionsIndex = false ;
873- if (indexPruningOptions .prune == null && indexPruningOptions .pruningConfig == PruningConfig .NULL ) {
874- usePreIndexOptionsIndex = randomBoolean ();
875- }
876-
877- IndexVersion indexVersion = getIndexVersionForTest (usePreIndexOptionsIndex );
878- MapperService mapperService = createMapperService (indexVersion , getIndexMapping (indexPruningOptions , usePreIndexOptionsIndex ));
879- Tuple <Boolean , TokenPruningConfig > queryPruneConfig = getQueryPruneConfig (queryPruningOptions );
845+ IndexVersion indexVersion = getIndexVersionForTest (randomBoolean ());
846+ MapperService mapperService = createMapperService (indexVersion , getIndexMapping (indexPruningOptions ));
880847 PruningScenario effectivePruningScenario = getEffectivePruningScenario (
881848 indexPruningOptions ,
882849 queryPruningOptions ,
883- usePreIndexOptionsIndex
850+ indexVersion
884851 );
885852 withSearchExecutionContext (mapperService , (context ) -> {
886853 SparseVectorFieldMapper .SparseVectorFieldType ft = (SparseVectorFieldMapper .SparseVectorFieldType ) mapperService .fieldType (
@@ -891,8 +858,8 @@ private void assertPruningScenario(PruningOptions indexPruningOptions, PruningOp
891858 context ,
892859 "field" ,
893860 QUERY_VECTORS ,
894- queryPruneConfig . v1 () ,
895- queryPruneConfig . v2 ()
861+ queryPruningOptions . prune ,
862+ queryPruningOptions . pruningConfig . tokenPruningConfig
896863 );
897864 assertQueryContains (expectedQueryClauses , finalizedQuery );
898865 });
0 commit comments