@@ -155,19 +155,26 @@ protected Object simulateMethod(Method method, Object[] args) {
155155
156156 @ Override
157157 protected void initializeAdditionalMappings (MapperService mapperService ) throws IOException {
158- if (shouldInjectSparseVectorIndexOptions ()) {
159- addSparseVectorIndexOptionsMapping (mapperService );
160- return ;
161- }
162-
163158 mapperService .merge (
164159 "_doc" ,
165- new CompressedXContent (Strings . toString ( PutMappingRequest . simpleMapping ( SPARSE_VECTOR_FIELD , "type=sparse_vector" ) )),
160+ new CompressedXContent (getTestSparseVectorIndexMapping ( )),
166161 MapperService .MergeReason .MAPPING_UPDATE
167162 );
168163 }
169164
170- private boolean shouldInjectSparseVectorIndexOptions () {
165+ private String getTestSparseVectorIndexMapping () {
166+ if (currentTestHasIndexOptions ()) {
167+ return "{\" properties\" :{\" "
168+ + SPARSE_VECTOR_FIELD
169+ + "\" :{\" type\" :\" sparse_vector\" ,\" index_options\" "
170+ + ":{\" prune\" :true,\" pruning_config\" :{\" tokens_freq_ratio_threshold\" "
171+ + ":12,\" tokens_weight_threshold\" :0.6}}}}}" ;
172+ }
173+
174+ return Strings .toString (PutMappingRequest .simpleMapping (SPARSE_VECTOR_FIELD , "type=sparse_vector" ));
175+ }
176+
177+ private boolean currentTestHasIndexOptions () {
171178 Class <?> clazz = this .getClass ();
172179 Class <InjectSparseVectorIndexOptions > injectSparseVectorIndexOptions = InjectSparseVectorIndexOptions .class ;
173180
@@ -179,15 +186,6 @@ private boolean shouldInjectSparseVectorIndexOptions() {
179186 }
180187 }
181188
182- private void addSparseVectorIndexOptionsMapping (MapperService mapperService ) throws IOException {
183- String addIndexOptionsTemplate = "{\" properties\" :{\" "
184- + SPARSE_VECTOR_FIELD
185- + "\" :{\" type\" :\" sparse_vector\" ,\" index_options\" "
186- + ":{\" prune\" :true,\" pruning_config\" :{\" tokens_freq_ratio_threshold\" "
187- + ":12,\" tokens_weight_threshold\" :0.6}}}}}" ;
188- mapperService .merge ("_doc" , new CompressedXContent (addIndexOptionsTemplate ), MapperService .MergeReason .MAPPING_UPDATE );
189- }
190-
191189 @ Override
192190 protected void doAssertLuceneQuery (SparseVectorQueryBuilder queryBuilder , Query query , SearchExecutionContext context ) {
193191 assertThat (query , instanceOf (SparseVectorQueryWrapper .class ));
@@ -283,7 +281,7 @@ private void testDoToQuery(SparseVectorQueryBuilder queryBuilder, SearchExecutio
283281
284282 assertTrue (query instanceof SparseVectorQueryWrapper );
285283 var sparseQuery = (SparseVectorQueryWrapper ) query ;
286- if (queryBuilder .shouldPruneTokens ()) {
284+ if (queryBuilder .shouldPruneTokens () || currentTestHasIndexOptions () ) {
287285 // It's possible that all documents were pruned for aggressive pruning configurations
288286 assertTrue (sparseQuery .getTermsQuery () instanceof BooleanQuery || sparseQuery .getTermsQuery () instanceof MatchNoDocsQuery );
289287 } else {
@@ -386,31 +384,33 @@ public void testThatWeCorrectlyRewriteQueryIntoVectors() {
386384 public void testItUsesIndexOptionsDefaults () throws IOException {
387385 withSearchIndex ((context ) -> {
388386 try {
389- SparseVectorQueryBuilder builder = new SparseVectorQueryBuilder (
390- SPARSE_VECTOR_FIELD ,
391- WEIGHTED_TOKENS ,
392- null ,
393- null ,
394- null ,
395- null
396- );
397- Query query = builder .doToQuery (context );
398-
387+ SparseVectorQueryBuilder builder = createTestQueryBuilder (null );
388+ assertFalse (builder .shouldPruneTokens ());
389+ testDoToQuery (builder , context );
399390 } catch (IOException ex ) {
400391 throw new RuntimeException (ex );
401392 }
402393 });
403394 }
404395
405396 @ InjectSparseVectorIndexOptions
406- public void testItOverridesIndexOptionsDefaults () {
407-
397+ public void testItOverridesIndexOptionsDefaults () throws IOException {
398+ withSearchIndex ((context ) -> {
399+ try {
400+ TokenPruningConfig pruningConfig = new TokenPruningConfig (2 , 0.3f , false );
401+ SparseVectorQueryBuilder builder = createTestQueryBuilder (pruningConfig );
402+ assertTrue (builder .shouldPruneTokens ());
403+ testDoToQuery (builder , context );
404+ } catch (IOException ex ) {
405+ throw new RuntimeException (ex );
406+ }
407+ });
408408 }
409409
410410 @ InjectSparseVectorIndexOptions
411- public void testToQueryWithIndexOptions () throws IOException {
411+ public void testToQueryRewriteWithIndexOptions () throws IOException {
412412 withSearchIndex ((context ) -> {
413- SparseVectorQueryBuilder queryBuilder = createTestQueryBuilder ();
413+ SparseVectorQueryBuilder queryBuilder = createTestQueryBuilder (null );
414414 try {
415415 if (queryBuilder .getQueryVectors () == null ) {
416416 QueryBuilder rewrittenQueryBuilder = rewriteAndFetch (queryBuilder , context );
@@ -424,15 +424,4 @@ public void testToQueryWithIndexOptions() throws IOException {
424424 }
425425 });
426426 }
427-
428- @ InjectSparseVectorIndexOptions
429- public void testWeCorrectlyRewriteQueryIntoVectorsWithIndexOptions () {
430- SearchExecutionContext searchExecutionContext = createSearchExecutionContext ();
431-
432- SparseVectorQueryBuilder queryBuilder = createTestQueryBuilder (null );
433- QueryBuilder rewrittenQueryBuilder = rewriteAndFetch (queryBuilder , searchExecutionContext );
434- assertTrue (rewrittenQueryBuilder instanceof SparseVectorQueryBuilder );
435- assertEquals (queryBuilder .shouldPruneTokens (), ((SparseVectorQueryBuilder ) rewrittenQueryBuilder ).shouldPruneTokens ());
436- assertNotNull (((SparseVectorQueryBuilder ) rewrittenQueryBuilder ).getQueryVectors ());
437- }
438427}
0 commit comments