fix range query rewrite so it rewrites correctly when shard min value == shard max value

This commit is contained in:
Colin Goodheart-Smithe 2016-03-17 10:24:11 +00:00
parent c62eff0ac9
commit d17fd335e4
2 changed files with 12 additions and 15 deletions

View File

@ -265,7 +265,7 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
// rewrite so just return without rewriting
if (fieldStatsProvider != null) {
DateMathParser dateMathParser = format == null ? null : new DateMathParser(format);
FieldStatsProvider.Relation relation = fieldStatsProvider.isFieldWithinQuery(fieldName, from, to, includeUpper, includeLower,
FieldStatsProvider.Relation relation = fieldStatsProvider.isFieldWithinQuery(fieldName, from, to, includeLower, includeUpper,
timeZone, dateMathParser);
switch (relation) {
case DISJOINT:
@ -278,15 +278,10 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
// bound has not been changed by the rewrite
RangeQueryBuilder newRangeQuery = new RangeQueryBuilder(fieldName);
String dateFormatString = format == null ? null : format.format();
if (fieldStats.getMinValue().equals(fieldStats.getMaxValue())) {
newRangeQuery.from(fieldStats.getMinValue(), true);
newRangeQuery.to(fieldStats.getMaxValue(), true);
} else {
newRangeQuery.from(fieldStats.getMinValue(), includeLower || fieldStats.match(new IndexConstraint(fieldName,
Property.MIN, Comparison.GT, fieldStats.stringValueOf(from, dateFormatString))));
newRangeQuery.to(fieldStats.getMaxValue(), includeUpper || fieldStats.match(new IndexConstraint(fieldName,
Property.MAX, Comparison.LT, fieldStats.stringValueOf(to, dateFormatString))));
}
newRangeQuery.from(fieldStats.getMinValue(), includeLower || fieldStats.match(
new IndexConstraint(fieldName, Property.MIN, Comparison.GT, fieldStats.stringValueOf(from, dateFormatString))));
newRangeQuery.to(fieldStats.getMaxValue(), includeUpper || fieldStats.match(
new IndexConstraint(fieldName, Property.MAX, Comparison.LT, fieldStats.stringValueOf(to, dateFormatString))));
newRangeQuery.format = format;
newRangeQuery.timeZone = timeZone;
return newRangeQuery;

View File

@ -53,15 +53,17 @@ public class MatchedQueriesIT extends ESIntegTestCase {
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().should(rangeQuery("number").lte(2).queryName("test1")).should(rangeQuery("number").gt(2).queryName("test2")))).get();
.setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery()
.should(rangeQuery("number").lt(2).queryName("test1")).should(rangeQuery("number").gte(2).queryName("test2"))))
.get();
assertHitCount(searchResponse, 3L);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1") || hit.id().equals("2")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("test1"));
} else if (hit.id().equals("3")) {
if (hit.id().equals("3") || hit.id().equals("2")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("test2"));
} else if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("test1"));
} else {
fail("Unexpected document returned with id " + hit.id());
}