Skip to content

Commit c1d40b5

Browse files
committed
test: fix SparseVectorFieldMapperTests edge case
1 parent c7fcf8b commit c1d40b5

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

server/src/test/java/org/elasticsearch/index/mapper/vectors/SparseVectorFieldMapperTests.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.inference.WeightedToken;
4141
import org.elasticsearch.search.lookup.Source;
4242
import org.elasticsearch.search.vectors.SparseVectorQueryWrapper;
43+
import org.elasticsearch.test.ESTestCase;
4344
import org.elasticsearch.test.index.IndexVersionUtils;
4445
import org.elasticsearch.xcontent.ToXContent;
4546
import org.elasticsearch.xcontent.XContentBuilder;
@@ -61,6 +62,7 @@
6162
import java.util.stream.Stream;
6263

6364
import static org.elasticsearch.index.IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT;
65+
import static org.elasticsearch.index.IndexVersions.SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X;
6466
import static org.elasticsearch.index.IndexVersions.UPGRADE_TO_LUCENE_10_0_0;
6567
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.NEW_SPARSE_VECTOR_INDEX_VERSION;
6668
import static org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.PREVIOUS_SPARSE_VECTOR_INDEX_VERSION;
@@ -749,9 +751,11 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
749751

750752
iw.addDocument(mapper.parse(source(b -> b.field("field", RARE_TOKENS))).rootDoc());
751753

752-
// This will lower the averageTokenFreqRatio so that common tokens get pruned with default settings
754+
// This will lower the averageTokenFreqRatio so that common tokens get pruned with default settings.
755+
// Depending on how the index is created, we will have 30-37 numUniqueTokens
756+
// this will result in an averageTokenFreqRatio of 0.1021 - 0.1259
753757
Map<String, Float> uniqueDoc = new TreeMap<>();
754-
for (int i = 0; i < 20; i++) {
758+
for (int i = 0; i < 30; i++) {
755759
uniqueDoc.put("unique" + i, 0.5f);
756760
}
757761
iw.addDocument(mapper.parse(source(b -> b.field("field", uniqueDoc))).rootDoc());
@@ -765,10 +769,10 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
765769
}
766770

767771
public void testPruningScenarios() throws Exception {
768-
for (int i = 0; i < 120; i++) {
772+
for (int i = 0; i < 200; i++) {
769773
assertPruningScenario(
770774
randomFrom(validIndexPruningScenarios),
771-
new PruningOptions(randomBoolean() ? randomBoolean() : null, randomFrom(PruningConfig.values()))
775+
new PruningOptions(randomFrom(true, false, null), randomFrom(PruningConfig.values()))
772776
);
773777
}
774778
}
@@ -798,7 +802,7 @@ private PruningScenario getEffectivePruningScenario(
798802
}
799803

800804
if (shouldPrune == null) {
801-
shouldPrune = indexVersion.onOrAfter(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT);
805+
shouldPrune = indexVersion.between(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X, IndexVersions.UPGRADE_TO_LUCENE_10_0_0) || indexVersion.onOrAfter(SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT);
802806
}
803807

804808
PruningScenario pruningScenario = PruningScenario.NO_PRUNING;
@@ -836,7 +840,10 @@ private List<Query> getExpectedQueryClauses(
836840
}
837841

838842
private void assertPruningScenario(PruningOptions indexPruningOptions, PruningOptions queryPruningOptions) throws IOException {
839-
IndexVersion indexVersion = getIndexVersionForTest(randomBoolean());
843+
IndexVersion indexVersion = ESTestCase.randomFrom(IndexVersionUtils.allReleasedVersions().subSet(
844+
SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_SUPPORT_BACKPORT_8_X,
845+
IndexVersion.current()
846+
));
840847
MapperService mapperService = createMapperService(indexVersion, getIndexMapping(indexPruningOptions));
841848
PruningScenario effectivePruningScenario = getEffectivePruningScenario(indexPruningOptions, queryPruningOptions, indexVersion);
842849
withSearchExecutionContext(mapperService, (context) -> {

0 commit comments

Comments
 (0)