diff --git a/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index 9c671fa2edc..02ca18c5777 100644 --- a/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -284,32 +284,29 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder FieldMapper mapper = parseContext.fieldMapper(this.fieldName); if (mapper != null) { if (mapper instanceof DateFieldMapper) { - if ((from instanceof Number || to instanceof Number) && timeZone != null) { - throw new QueryParsingException(parseContext, - "[range] time_zone when using ms since epoch format as it's UTC based can not be applied to [" + this.fieldName - + "]"); - } DateMathParser forcedDateParser = null; if (this.format != null) { - forcedDateParser = new DateMathParser(Joda.forPattern(this.format), DateFieldMapper.Defaults.TIME_UNIT); + forcedDateParser = new DateMathParser(Joda.forPattern(this.format)); } DateTimeZone dateTimeZone = null; if (this.timeZone != null) { dateTimeZone = DateTimeZone.forID(this.timeZone); } - query = ((DateFieldMapper) mapper).rangeQuery(from, to, includeLower, includeUpper, dateTimeZone, forcedDateParser, parseContext); + query = ((DateFieldMapper) mapper).fieldType().rangeQuery(from, to, includeLower, includeUpper, dateTimeZone, forcedDateParser, parseContext); } else { if (timeZone != null) { throw new QueryParsingException(parseContext, "[range] time_zone can not be applied to non date field [" - + this.fieldName + "]"); + + fieldName + "]"); } //LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well? query = mapper.rangeQuery(from, to, includeLower, includeUpper, parseContext); } } + if (query == null) { query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper); } + query.setBoost(boost); if (queryName != null) { parseContext.addNamedQuery(queryName, query); diff --git a/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java index 564ade46dca..ebbc4fc86c4 100644 --- a/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -71,7 +71,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder @Override protected Query createExpectedQuery(RangeQueryBuilder queryBuilder, QueryParseContext context) throws IOException { Query expectedQuery; - if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false) ) { - expectedQuery = new TermRangeQuery(queryBuilder.fieldName(), + String fieldName = queryBuilder.fieldName(); + if (getCurrentTypes().length == 0 || (fieldName.equals(DATE_FIELD_NAME) == false && fieldName.equals(INT_FIELD_NAME) == false) ) { + expectedQuery = new TermRangeQuery(fieldName, BytesRefs.toBytesRef(queryBuilder.from()), BytesRefs.toBytesRef(queryBuilder.to()), queryBuilder.includeLower(), queryBuilder.includeUpper()); - } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) { - DateFieldMapper.Builder fieldMapperBuilder = new DateFieldMapper.Builder(queryBuilder.fieldName()); - DateFieldMapper fieldMapper = fieldMapperBuilder.build(new Mapper.BuilderContext(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(), new ContentPath())); - DateMathParser dateMathParser = null; - if (queryBuilder.format() != null) { - dateMathParser = new DateMathParser(Joda.forPattern(queryBuilder.format()), DateFieldMapper.Defaults.TIME_UNIT); + } else if (fieldName.equals(DATE_FIELD_NAME)) { + DateMathParser forcedDateParser = null; + if (queryBuilder.format() != null) { + forcedDateParser = new DateMathParser(Joda.forPattern(queryBuilder.format())); } - DateTimeZone timeZone = null; - if( queryBuilder.timeZone() != null) { - timeZone = DateTimeZone.forID(queryBuilder.timeZone()); + DateTimeZone dateTimeZone = null; + if (queryBuilder.timeZone() != null) { + dateTimeZone = DateTimeZone.forID(queryBuilder.timeZone()); } - Long from = null; - if (queryBuilder.from() != null) { - from = fieldMapper.parseToMilliseconds(queryBuilder.from(), queryBuilder.includeLower(), timeZone, dateMathParser); - } - Long to = null; - if (queryBuilder.to() != null) { - to = fieldMapper.parseToMilliseconds(queryBuilder.to(), queryBuilder.includeLower(), timeZone, dateMathParser); - } - expectedQuery = fieldMapper.rangeQuery(from, to, queryBuilder.includeLower(), queryBuilder.includeUpper(), timeZone, dateMathParser, context); + FieldMapper mapper = context.fieldMapper(queryBuilder.fieldName()); + expectedQuery = ((DateFieldMapper) mapper).fieldType().rangeQuery(queryBuilder.from(), queryBuilder.to(), queryBuilder.includeLower(), queryBuilder.includeUpper(), dateTimeZone, forcedDateParser, context); } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) { expectedQuery = NumericRangeQuery.newIntRange(INT_FIELD_NAME, (Integer) queryBuilder.from(), (Integer) queryBuilder.to(), queryBuilder.includeLower(), queryBuilder.includeUpper()); } else { @@ -167,16 +162,6 @@ public class RangeQueryBuilderTest extends BaseQueryTestCase query.toQuery(createContext()); } - /** - * Specifying a timezone together with a numeric to or from fields should throw an error. - */ - @Test(expected=QueryParsingException.class) - public void testToQueryNumericFromAndTimezone() throws QueryParsingException, IOException { - RangeQueryBuilder query = new RangeQueryBuilder(DATE_FIELD_NAME); - query.from(1).to(10).timeZone("UTC"); - query.toQuery(createContext()); - } - @Override protected RangeQueryBuilder createEmptyQueryBuilder() { return new RangeQueryBuilder(null);