Skip to content

Commit

Permalink
RangeQuery WITHIN case now normalises query (#22431)
Browse files Browse the repository at this point in the history
Previous to his change when the range query was rewritten to an unbounded range (`[* TO *]`) it maintained the timezone and format for the query. This means that queries with different timezones and format which are rewritten to unbounded range queries actually end up as different entries in the search request cache.

This is inefficient and unnecessary so this change nulls the timezone and format in the rewritten query so that regardless of the timezone or format the rewritten query will be the same.

Although this does not fix #22412 (since it deals with the WITHIN case rather than the INTERSECTS case) it is born from the same arguments
  • Loading branch information
colings86 committed Jan 25, 2017
1 parent dc3834f commit 0bbdade
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
case DISJOINT:
return new MatchNoneQueryBuilder();
case WITHIN:
if (from != null || to != null) {
if (from != null || to != null || format != null || timeZone != null) {
RangeQueryBuilder newRangeQuery = new RangeQueryBuilder(fieldName);
newRangeQuery.from(null);
newRangeQuery.to(null);
newRangeQuery.format = format;
newRangeQuery.timeZone = timeZone;
newRangeQuery.format = null;
newRangeQuery.timeZone = null;
return newRangeQuery;
} else {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.query;

import com.carrotsearch.randomizedtesting.generators.RandomPicks;

import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.search.LegacyNumericRangeQuery;
Expand Down Expand Up @@ -503,6 +504,31 @@ protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteC
assertThat(rewrittenRange.to(), equalTo(null));
}

public void testRewriteDateToMatchAllWithTimezoneAndFormat() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
@Override
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
return Relation.WITHIN;
}
};
DateTime queryFromValue = new DateTime(2015, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC());
DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC());
query.from(queryFromValue);
query.to(queryToValue);
query.timeZone(randomFrom(DateTimeZone.getAvailableIDs()));
query.format("yyyy-MM-dd");
QueryShardContext queryShardContext = createShardContext();
QueryBuilder rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, instanceOf(RangeQueryBuilder.class));
RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten;
assertThat(rewrittenRange.fieldName(), equalTo(fieldName));
assertThat(rewrittenRange.from(), equalTo(null));
assertThat(rewrittenRange.to(), equalTo(null));
assertThat(rewrittenRange.timeZone(), equalTo(null));
assertThat(rewrittenRange.format(), equalTo(null));
}

public void testRewriteDateToMatchNone() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
Expand Down

0 comments on commit 0bbdade

Please sign in to comment.