40
40
import org .elasticsearch .inference .WeightedToken ;
41
41
import org .elasticsearch .search .lookup .Source ;
42
42
import org .elasticsearch .search .vectors .SparseVectorQueryWrapper ;
43
+ import org .elasticsearch .test .ESTestCase ;
43
44
import org .elasticsearch .test .index .IndexVersionUtils ;
44
45
import org .elasticsearch .xcontent .ToXContent ;
45
46
import org .elasticsearch .xcontent .XContentBuilder ;
55
56
import java .util .LinkedHashMap ;
56
57
import java .util .List ;
57
58
import java .util .Map ;
59
+ import java .util .NavigableSet ;
58
60
import java .util .Set ;
59
61
import java .util .TreeMap ;
60
62
import java .util .stream .Collectors ;
61
63
import java .util .stream .Stream ;
62
64
65
+ import static org .elasticsearch .index .IndexVersions .NEW_SPARSE_VECTOR ;
63
66
import static org .elasticsearch .index .IndexVersions .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT ;
67
+ import static org .elasticsearch .index .IndexVersions .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X ;
64
68
import static org .elasticsearch .index .IndexVersions .UPGRADE_TO_LUCENE_10_0_0 ;
65
- import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .NEW_SPARSE_VECTOR_INDEX_VERSION ;
66
- import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .PREVIOUS_SPARSE_VECTOR_INDEX_VERSION ;
67
- import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION ;
68
69
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertToXContentEquivalent ;
69
70
import static org .elasticsearch .xcontent .XContentFactory .jsonBuilder ;
70
71
import static org .hamcrest .Matchers .containsString ;
@@ -286,7 +287,7 @@ public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion() throws
286
287
IndexVersion indexVersion = IndexVersionUtils .randomVersionBetween (
287
288
random (),
288
289
UPGRADE_TO_LUCENE_10_0_0 ,
289
- IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION )
290
+ IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT )
290
291
);
291
292
292
293
XContentBuilder orig = JsonXContent .contentBuilder ().startObject ();
@@ -501,21 +502,21 @@ protected IngestScriptSupport ingestScriptSupport() {
501
502
@ Override
502
503
protected String [] getParseMinimalWarnings (IndexVersion indexVersion ) {
503
504
String [] additionalWarnings = null ;
504
- if (indexVersion .before (PREVIOUS_SPARSE_VECTOR_INDEX_VERSION )) {
505
+ if (indexVersion .before (IndexVersions . V_8_0_0 )) {
505
506
additionalWarnings = new String [] { SparseVectorFieldMapper .ERROR_MESSAGE_7X };
506
507
}
507
508
return Strings .concatStringArrays (super .getParseMinimalWarnings (indexVersion ), additionalWarnings );
508
509
}
509
510
510
511
@ Override
511
512
protected IndexVersion boostNotAllowedIndexVersion () {
512
- return NEW_SPARSE_VECTOR_INDEX_VERSION ;
513
+ return NEW_SPARSE_VECTOR ;
513
514
}
514
515
515
516
public void testSparseVectorUnsupportedIndex () {
516
517
IndexVersion version = IndexVersionUtils .randomVersionBetween (
517
518
random (),
518
- PREVIOUS_SPARSE_VECTOR_INDEX_VERSION ,
519
+ IndexVersions . V_8_0_0 ,
519
520
IndexVersions .FIRST_DETACHED_INDEX_VERSION
520
521
);
521
522
Exception e = expectThrows (MapperParsingException .class , () -> createMapperService (version , fieldMapping (b -> {
@@ -742,9 +743,11 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
742
743
743
744
iw .addDocument (mapper .parse (source (b -> b .field ("field" , RARE_TOKENS ))).rootDoc ());
744
745
745
- // This will lower the averageTokenFreqRatio so that common tokens get pruned with default settings
746
+ // This will lower the averageTokenFreqRatio so that common tokens get pruned with default settings.
747
+ // Depending on how the index is created, we will have 30-37 numUniqueTokens
748
+ // this will result in an averageTokenFreqRatio of 0.1021 - 0.1259
746
749
Map <String , Float > uniqueDoc = new TreeMap <>();
747
- for (int i = 0 ; i < 20 ; i ++) {
750
+ for (int i = 0 ; i < 30 ; i ++) {
748
751
uniqueDoc .put ("unique" + i , 0.5f );
749
752
}
750
753
iw .addDocument (mapper .parse (source (b -> b .field ("field" , uniqueDoc ))).rootDoc ());
@@ -758,10 +761,10 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
758
761
}
759
762
760
763
public void testPruningScenarios () throws Exception {
761
- for (int i = 0 ; i < 120 ; i ++) {
764
+ for (int i = 0 ; i < 200 ; i ++) {
762
765
assertPruningScenario (
763
766
randomFrom (validIndexPruningScenarios ),
764
- new PruningOptions (randomBoolean () ? randomBoolean () : null , randomFrom (PruningConfig .values ()))
767
+ new PruningOptions (randomFrom ( true , false , null ) , randomFrom (PruningConfig .values ()))
765
768
);
766
769
}
767
770
}
@@ -791,7 +794,8 @@ private PruningScenario getEffectivePruningScenario(
791
794
}
792
795
793
796
if (shouldPrune == null ) {
794
- shouldPrune = indexVersion .onOrAfter (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT );
797
+ shouldPrune = indexVersion .between (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X , UPGRADE_TO_LUCENE_10_0_0 )
798
+ || indexVersion .onOrAfter (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT );
795
799
}
796
800
797
801
PruningScenario pruningScenario = PruningScenario .NO_PRUNING ;
@@ -829,7 +833,7 @@ private List<Query> getExpectedQueryClauses(
829
833
}
830
834
831
835
private void assertPruningScenario (PruningOptions indexPruningOptions , PruningOptions queryPruningOptions ) throws IOException {
832
- IndexVersion indexVersion = getIndexVersionForTest ( randomBoolean () );
836
+ IndexVersion indexVersion = getIndexVersion ( );
833
837
MapperService mapperService = createMapperService (indexVersion , getIndexMapping (indexPruningOptions ));
834
838
PruningScenario effectivePruningScenario = getEffectivePruningScenario (indexPruningOptions , queryPruningOptions , indexVersion );
835
839
withSearchExecutionContext (mapperService , (context ) -> {
@@ -848,14 +852,41 @@ private void assertPruningScenario(PruningOptions indexPruningOptions, PruningOp
848
852
});
849
853
}
850
854
851
- private IndexVersion getIndexVersionForTest (boolean usePreviousIndex ) {
852
- return usePreviousIndex
853
- ? IndexVersionUtils .randomVersionBetween (
854
- random (),
855
- UPGRADE_TO_LUCENE_10_0_0 ,
856
- IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT )
857
- )
858
- : IndexVersionUtils .randomVersionBetween (random (), SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT , IndexVersion .current ());
855
+ private static IndexVersion getIndexVersion () {
856
+ VersionRange versionRange = randomFrom (VersionRange .values ());
857
+ return versionRange .getRandomVersion ();
858
+ }
859
+
860
+ private enum VersionRange {
861
+ ES_V8X_WITHOUT_INDEX_OPTIONS_SUPPORT (
862
+ NEW_SPARSE_VECTOR ,
863
+ IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X )
864
+ ),
865
+ ES_V8X_WITH_INDEX_OPTIONS_SUPPORT (
866
+ SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X ,
867
+ IndexVersionUtils .getPreviousVersion (UPGRADE_TO_LUCENE_10_0_0 )
868
+ ),
869
+ ES_V9X_WITHOUT_INDEX_OPTIONS_SUPPORT (
870
+ UPGRADE_TO_LUCENE_10_0_0 ,
871
+ IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT )
872
+ ),
873
+ ES_V9X_WITH_INDEX_OPTIONS_SUPPORT (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT , IndexVersion .current ());
874
+
875
+ private final IndexVersion fromVersion ;
876
+ private final IndexVersion toVersion ;
877
+
878
+ VersionRange (IndexVersion fromVersion , IndexVersion toVersion ) {
879
+ this .fromVersion = fromVersion ;
880
+ this .toVersion = toVersion ;
881
+ }
882
+
883
+ IndexVersion getRandomVersion () {
884
+ // TODO: replace implementation with `IndexVersionUtils::randomVersionBetween` once support is added
885
+ // for handling unbalanced version distributions.
886
+ NavigableSet <IndexVersion > allReleaseVersions = IndexVersionUtils .allReleasedVersions ();
887
+ Set <IndexVersion > candidateVersions = allReleaseVersions .subSet (fromVersion , toVersion );
888
+ return ESTestCase .randomFrom (candidateVersions );
889
+ }
859
890
}
860
891
861
892
private static final List <WeightedToken > QUERY_VECTORS = Stream .of (RARE_TOKENS , MEDIUM_TOKENS , COMMON_TOKENS )
0 commit comments