@@ -63,12 +63,21 @@ public class SparseVectorQueryBuilderTests extends AbstractQueryTestCase<SparseV
6363 private static final String SPARSE_VECTOR_FIELD = "mySparseVectorField" ;
6464 private static final List <WeightedToken > WEIGHTED_TOKENS = List .of (new WeightedToken ("foo" , .42f ));
6565 private static final int NUM_TOKENS = WEIGHTED_TOKENS .size ();
66+ private final IndexVersion indexVersionToTest ;
6667
6768 @ Retention (RetentionPolicy .RUNTIME )
6869 @ Target (ElementType .METHOD )
6970 public @interface InjectSparseVectorIndexOptions {
7071 }
7172
73+ public SparseVectorQueryBuilderTests () {
74+ // The sparse_vector field is not supported on versions 8.0 to 8.10. Because of this we'll only allow
75+ // index versions after its reintroduction.
76+ indexVersionToTest = randomBoolean ()
77+ ? IndexVersion .current ()
78+ : IndexVersionUtils .randomVersionBetween (random (), IndexVersions .NEW_SPARSE_VECTOR , IndexVersion .current ());
79+ }
80+
7281 @ Override
7382 protected SparseVectorQueryBuilder doCreateTestQueryBuilder () {
7483 TokenPruningConfig tokenPruningConfig = randomBoolean ()
@@ -115,12 +124,7 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
115124
116125 @ Override
117126 protected Settings createTestIndexSettings () {
118- // The sparse_vector field is not supported on versions 8.0 to 8.10. Because of this we'll only allow
119- // index versions after its reintroduction.
120- final IndexVersion indexVersionCreated = randomBoolean ()
121- ? IndexVersion .current ()
122- : IndexVersionUtils .randomVersionBetween (random (), IndexVersions .NEW_SPARSE_VECTOR , IndexVersion .current ());
123- return Settings .builder ().put (IndexMetadata .SETTING_VERSION_CREATED , indexVersionCreated ).build ();
127+ return Settings .builder ().put (IndexMetadata .SETTING_VERSION_CREATED , indexVersionToTest ).build ();
124128 }
125129
126130 @ Override
@@ -171,6 +175,10 @@ private String getTestSparseVectorIndexMapping() {
171175 }
172176
173177 private boolean currentTestHasIndexOptions () {
178+ if (indexVersionSupportsIndexOptions () == false ) {
179+ return false ;
180+ }
181+
174182 Class <?> clazz = this .getClass ();
175183 Class <InjectSparseVectorIndexOptions > injectSparseVectorIndexOptions = InjectSparseVectorIndexOptions .class ;
176184
@@ -182,6 +190,18 @@ private boolean currentTestHasIndexOptions() {
182190 }
183191 }
184192
193+ private boolean indexVersionSupportsIndexOptions () {
194+ if (indexVersionToTest .onOrAfter (IndexVersions .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT )) {
195+ return true ;
196+ }
197+
198+ if (indexVersionToTest .between (IndexVersions .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X , IndexVersions .UPGRADE_TO_LUCENE_10_0_0 )) {
199+ return true ;
200+ }
201+
202+ return false ;
203+ }
204+
185205 @ Override
186206 protected void doAssertLuceneQuery (SparseVectorQueryBuilder queryBuilder , Query query , SearchExecutionContext context ) {
187207 assertThat (query , instanceOf (SparseVectorQueryWrapper .class ));
@@ -277,6 +297,8 @@ private void testDoToQuery(SparseVectorQueryBuilder queryBuilder, SearchExecutio
277297
278298 assertTrue (query instanceof SparseVectorQueryWrapper );
279299 var sparseQuery = (SparseVectorQueryWrapper ) query ;
300+
301+ // check if we have explicit pruning, or pruning via the index_options
280302 if (queryBuilder .shouldPruneTokens () || currentTestHasIndexOptions ()) {
281303 // It's possible that all documents were pruned for aggressive pruning configurations
282304 assertTrue (sparseQuery .getTermsQuery () instanceof BooleanQuery || sparseQuery .getTermsQuery () instanceof MatchNoDocsQuery );
0 commit comments