Skip to content

Commit

Permalink
Fix rare failure condition in FieldSortBuilderTests (#74347) (#74356)
Browse files Browse the repository at this point in the history
Under rare randomization conditions, the mininum values used in
FieldSortBuilderTests#testIsBottomSortShardDisjoint can currently be
missintepreted and parsed as years by the date parser that is called deeper down
in DateFieldMapper#isFieldWithinQuery. In practice this cannot happen because
`isBottomSortShardDisjoint` uses the formatted sort values for a date field,
which are string values and don't cause this kind of error. This PR fixes that
in the test setup.

Closes #74142
  • Loading branch information
Christoph Büscher committed Jun 21, 2021
1 parent 1d277b8 commit 98572f6
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
import org.elasticsearch.search.SearchSortValuesAndFormats;
Expand Down Expand Up @@ -636,8 +636,10 @@ public void testIsBottomSortShardDisjoint() throws Exception {
FieldSortBuilder fieldSort = SortBuilders.fieldSort("custom-date");
try (DirectoryReader reader = writer.getReader()) {
SearchExecutionContext context = createMockSearchExecutionContext(new IndexSearcher(reader));
DocValueFormat[] dateValueFormat = new DocValueFormat[] {
context.getFieldType("custom-date").docValueFormat(null, null) };
assertTrue(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { 0L }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { 0L }, dateValueFormat)));
}
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
Expand All @@ -650,27 +652,29 @@ public void testIsBottomSortShardDisjoint() throws Exception {
}
try (DirectoryReader reader = writer.getReader()) {
SearchExecutionContext context = createMockSearchExecutionContext(new IndexSearcher(reader));
DocValueFormat[] dateValueFormat = new DocValueFormat[] {
context.getFieldType("custom-date").docValueFormat(null, null) };
assertFalse(fieldSort.isBottomSortShardDisjoint(context, null));
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { minValue }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { minValue }, dateValueFormat)));
assertTrue(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { minValue-1 }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { minValue-1 }, dateValueFormat)));
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { minValue+1 }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { minValue+1 }, dateValueFormat)));
fieldSort.order(SortOrder.DESC);
assertTrue(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { maxValue+1 }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { maxValue+1 }, dateValueFormat)));
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { maxValue }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { maxValue }, dateValueFormat)));
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { minValue }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { minValue }, dateValueFormat)));
fieldSort.setNestedSort(new NestedSortBuilder("empty"));
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { minValue-1 }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { minValue-1 }, dateValueFormat)));
fieldSort.setNestedSort(null);
fieldSort.missing("100");
assertFalse(fieldSort.isBottomSortShardDisjoint(context,
new SearchSortValuesAndFormats(new Object[] { maxValue+1 }, new DocValueFormat[] { DocValueFormat.RAW })));
new SearchSortValuesAndFormats(new Object[] { maxValue+1 }, dateValueFormat)));
}
}
}
Expand Down

0 comments on commit 98572f6

Please sign in to comment.