diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index d37e7132d68..9c1610741d9 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -794,7 +794,6 @@ - diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java index 50b061337d1..1915efcb214 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java @@ -161,71 +161,6 @@ public class DateFieldMapper extends FieldMapper { } public static final class DateFieldType extends MappedFieldType { - - final class LateParsingQuery extends Query { - - final Object lowerTerm; - final Object upperTerm; - final boolean includeLower; - final boolean includeUpper; - final DateTimeZone timeZone; - final DateMathParser forcedDateParser; - private QueryShardContext queryShardContext; - - public LateParsingQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, - DateTimeZone timeZone, DateMathParser forcedDateParser, QueryShardContext queryShardContext) { - this.lowerTerm = lowerTerm; - this.upperTerm = upperTerm; - this.includeLower = includeLower; - this.includeUpper = includeUpper; - this.timeZone = timeZone; - this.forcedDateParser = forcedDateParser; - this.queryShardContext = queryShardContext; - } - - @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); - if (rewritten != this) { - return rewritten; - } - return innerRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, queryShardContext); - } - - // Even though we only cache rewritten queries it is good to let all queries implement hashCode() and equals(): - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (sameClassAs(o) == false) return false; - - LateParsingQuery that = (LateParsingQuery) o; - if (includeLower != that.includeLower) return false; - if (includeUpper != that.includeUpper) return false; - if (lowerTerm != null ? !lowerTerm.equals(that.lowerTerm) : that.lowerTerm != null) return false; - if (upperTerm != null ? !upperTerm.equals(that.upperTerm) : that.upperTerm != null) return false; - if (timeZone != null ? !timeZone.equals(that.timeZone) : that.timeZone != null) return false; - - return true; - } - - @Override - public int hashCode() { - return Objects.hash(classHash(), lowerTerm, upperTerm, includeLower, includeUpper, timeZone); - } - - @Override - public String toString(String s) { - final StringBuilder sb = new StringBuilder(); - return sb.append(name()).append(':') - .append(includeLower ? '[' : '{') - .append((lowerTerm == null) ? "*" : lowerTerm.toString()) - .append(" TO ") - .append((upperTerm == null) ? "*" : upperTerm.toString()) - .append(includeUpper ? ']' : '}') - .toString(); - } - } - protected FormatDateTimeFormatter dateTimeFormatter; protected DateMathParser dateMathParser; @@ -317,7 +252,7 @@ public class DateFieldMapper extends FieldMapper { public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable DateTimeZone timeZone, @Nullable DateMathParser forcedDateParser, QueryShardContext context) { failIfNotIndexed(); - return new LateParsingQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, context); + return innerRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, context); } Query innerRangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java index 82bcaed983b..328d16e1e95 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java @@ -176,70 +176,6 @@ public class LegacyDateFieldMapper extends LegacyNumberFieldMapper { public static class DateFieldType extends NumberFieldType { - final class LateParsingQuery extends Query { - - final Object lowerTerm; - final Object upperTerm; - final boolean includeLower; - final boolean includeUpper; - final DateTimeZone timeZone; - final DateMathParser forcedDateParser; - private QueryShardContext context; - - public LateParsingQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, DateTimeZone timeZone, - DateMathParser forcedDateParser, QueryShardContext context) { - this.lowerTerm = lowerTerm; - this.upperTerm = upperTerm; - this.includeLower = includeLower; - this.includeUpper = includeUpper; - this.timeZone = timeZone; - this.forcedDateParser = forcedDateParser; - this.context = context; - } - - @Override - public Query rewrite(IndexReader reader) throws IOException { - Query rewritten = super.rewrite(reader); - if (rewritten != this) { - return rewritten; - } - return innerRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, context); - } - - // Even though we only cache rewritten queries it is good to let all queries implement hashCode() and equals(): - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (sameClassAs(o) == false) return false; - - LateParsingQuery that = (LateParsingQuery) o; - if (includeLower != that.includeLower) return false; - if (includeUpper != that.includeUpper) return false; - if (lowerTerm != null ? !lowerTerm.equals(that.lowerTerm) : that.lowerTerm != null) return false; - if (upperTerm != null ? !upperTerm.equals(that.upperTerm) : that.upperTerm != null) return false; - if (timeZone != null ? !timeZone.equals(that.timeZone) : that.timeZone != null) return false; - - return true; - } - - @Override - public int hashCode() { - return Objects.hash(classHash(), lowerTerm, upperTerm, includeLower, includeUpper, timeZone); - } - - @Override - public String toString(String s) { - final StringBuilder sb = new StringBuilder(); - return sb.append(name()).append(':') - .append(includeLower ? '[' : '{') - .append((lowerTerm == null) ? "*" : lowerTerm.toString()) - .append(" TO ") - .append((upperTerm == null) ? "*" : upperTerm.toString()) - .append(includeUpper ? ']' : '}') - .toString(); - } - } - protected FormatDateTimeFormatter dateTimeFormatter = Defaults.DATE_TIME_FORMATTER; protected TimeUnit timeUnit = Defaults.TIME_UNIT; protected DateMathParser dateMathParser = new DateMathParser(dateTimeFormatter); @@ -371,7 +307,7 @@ public class LegacyDateFieldMapper extends LegacyNumberFieldMapper { public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable DateTimeZone timeZone, @Nullable DateMathParser forcedDateParser, QueryShardContext context) { - return new LateParsingQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, context); + return innerRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser, context); } private Query innerRangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, 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 210249c8c4a..19244317a5f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -260,6 +260,10 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i return this.timeZone == null ? null : this.timeZone.getID(); } + DateTimeZone getDateTimeZone() { // for testing + return timeZone; + } + /** * In case of format field, we can parse the from/to fields using this time format */ @@ -278,6 +282,13 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i return this.format == null ? null : this.format.format(); } + DateMathParser getForceDateParser() { // pkg private for testing + if (this.format != null) { + return new DateMathParser(this.format); + } + return null; + } + @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); @@ -440,19 +451,13 @@ public class RangeQueryBuilder extends AbstractQueryBuilder i MappedFieldType mapper = context.fieldMapper(this.fieldName); if (mapper != null) { if (mapper instanceof LegacyDateFieldMapper.DateFieldType) { - DateMathParser forcedDateParser = null; - if (this.format != null) { - forcedDateParser = new DateMathParser(this.format); - } + query = ((LegacyDateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, - timeZone, forcedDateParser, context); + timeZone, getForceDateParser(), context); } else if (mapper instanceof DateFieldMapper.DateFieldType) { - DateMathParser forcedDateParser = null; - if (this.format != null) { - forcedDateParser = new DateMathParser(this.format); - } + query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, - timeZone, forcedDateParser, context); + timeZone, getForceDateParser(), context); } else { if (timeZone != null) { throw new QueryShardException(context, "[range] time_zone can not be applied to non date field [" diff --git a/core/src/test/java/org/elasticsearch/index/mapper/LegacyDateFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/LegacyDateFieldMapperTests.java index 3d644a01af1..19f67c488b1 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/LegacyDateFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/LegacyDateFieldMapperTests.java @@ -256,7 +256,7 @@ public class LegacyDateFieldMapperTests extends ESSingleNodeTestCase { assertThat(((LegacyLongFieldMapper.CustomLongNumericField) doc.rootDoc().getField("date_field")).numericAsString(), equalTo(Long.toString(new DateTime(TimeValue.timeValueHours(10).millis(), DateTimeZone.UTC).getMillis()))); LegacyNumericRangeQuery rangeQuery = (LegacyNumericRangeQuery) defaultMapper.mappers().smartNameFieldMapper("date_field").fieldType() - .rangeQuery("10:00:00", "11:00:00", true, true, context).rewrite(null); + .rangeQuery("10:00:00", "11:00:00", true, true, context); assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(11).millis(), DateTimeZone.UTC).getMillis() + 999)); assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(10).millis(), DateTimeZone.UTC).getMillis())); } @@ -283,7 +283,7 @@ public class LegacyDateFieldMapperTests extends ESSingleNodeTestCase { assertThat(((LegacyLongFieldMapper.CustomLongNumericField) doc.rootDoc().getField("date_field")).numericAsString(), equalTo(Long.toString(new DateTime(TimeValue.timeValueHours(34).millis(), DateTimeZone.UTC).getMillis()))); LegacyNumericRangeQuery rangeQuery = (LegacyNumericRangeQuery) defaultMapper.mappers().smartNameFieldMapper("date_field").fieldType() - .rangeQuery("Jan 02 10:00:00", "Jan 02 11:00:00", true, true, context).rewrite(null); + .rangeQuery("Jan 02 10:00:00", "Jan 02 11:00:00", true, true, context); assertThat(rangeQuery.getMax(), equalTo(new DateTime(TimeValue.timeValueHours(35).millis() + 999, DateTimeZone.UTC).getMillis())); assertThat(rangeQuery.getMin(), equalTo(new DateTime(TimeValue.timeValueHours(34).millis(), DateTimeZone.UTC).getMillis())); } diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index f996de49ac7..60c17d88e65 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -29,8 +29,11 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.lucene.BytesRefs; +import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.index.mapper.LegacyDateFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.test.AbstractQueryTestCase; import org.joda.time.DateTime; @@ -118,7 +121,8 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase rewrittenQuery.rewrite(null)); + expectThrows(ElasticsearchParseException.class, () -> parseQuery(invalidQuery).toQuery(createShardContext())); } public void testDateRangeBoundaries() throws IOException { @@ -270,7 +330,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase") .postTags("") ).setQuery(QueryBuilders.boolQuery().must( - QueryBuilders.rangeQuery("d").gte("now-7d/d").lte("now").includeLower(true).includeUpper(true).boost(1.0f)) + QueryBuilders.rangeQuery("d").gte("now-12h").lte("now").includeLower(true).includeUpper(true).boost(1.0f)) .should(QueryBuilders.termQuery("field", "hello"))) .get(); assertSearchResponse(r1); - assertThat(r1.getHits().getTotalHits(), equalTo(3L)); + assertThat(r1.getHits().getTotalHits(), equalTo(1L)); assertHighlight(r1, 0, "field", 0, 1, equalTo("hello world")); }