diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index 2a627c54590..c3953a51170 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -265,7 +265,7 @@ public class RangeQueryBuilder extends AbstractQueryBuilder 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 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; diff --git a/core/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesIT.java b/core/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesIT.java index d93d5117274..00fa879f08f 100644 --- a/core/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesIT.java +++ b/core/src/test/java/org/elasticsearch/search/matchedqueries/MatchedQueriesIT.java @@ -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()); }