Fixing incompatibilities with changes on master in DateFieldMapper and RangeQueryParser

This commit is contained in:
Christoph Büscher 2015-06-04 17:11:40 +02:00
parent 313e9c6769
commit 2fc162a04d
3 changed files with 21 additions and 39 deletions

View File

@ -284,32 +284,29 @@ public class RangeQueryBuilder extends MultiTermQueryBuilder<RangeQueryBuilder>
FieldMapper mapper = parseContext.fieldMapper(this.fieldName); FieldMapper mapper = parseContext.fieldMapper(this.fieldName);
if (mapper != null) { if (mapper != null) {
if (mapper instanceof DateFieldMapper) { 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; DateMathParser forcedDateParser = null;
if (this.format != 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; DateTimeZone dateTimeZone = null;
if (this.timeZone != null) { if (this.timeZone != null) {
dateTimeZone = DateTimeZone.forID(this.timeZone); 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 { } else {
if (timeZone != null) { if (timeZone != null) {
throw new QueryParsingException(parseContext, "[range] time_zone can not be applied to non date field [" 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? //LUCENE 4 UPGRADE Mapper#rangeQuery should use bytesref as well?
query = mapper.rangeQuery(from, to, includeLower, includeUpper, parseContext); query = mapper.rangeQuery(from, to, includeLower, includeUpper, parseContext);
} }
} }
if (query == null) { if (query == null) {
query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper); query = new TermRangeQuery(this.fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper);
} }
query.setBoost(boost); query.setBoost(boost);
if (queryName != null) { if (queryName != null) {
parseContext.addNamedQuery(queryName, query); parseContext.addNamedQuery(queryName, query);

View File

@ -71,7 +71,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
String fieldName = this.fieldName; String fieldName = this.fieldName;
FieldMapper mapper = context.fieldMapper(fieldName); FieldMapper mapper = context.fieldMapper(fieldName);
if (mapper != null) { if (mapper != null) {
fieldName = mapper.names().indexName(); fieldName = mapper.fieldType().names().indexName();
valueBytes = mapper.indexedValueForSearch(value); valueBytes = mapper.indexedValueForSearch(value);
} }
if (valueBytes == null) { if (valueBytes == null) {

View File

@ -24,13 +24,16 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TermRangeQuery;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.DateMathParser;
import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.ContentPath; import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.mapper.core.DateFieldMapper.DateFieldType;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.junit.Test; import org.junit.Test;
@ -94,31 +97,23 @@ public class RangeQueryBuilderTest extends BaseQueryTestCase<RangeQueryBuilder>
@Override @Override
protected Query createExpectedQuery(RangeQueryBuilder queryBuilder, QueryParseContext context) throws IOException { protected Query createExpectedQuery(RangeQueryBuilder queryBuilder, QueryParseContext context) throws IOException {
Query expectedQuery; Query expectedQuery;
if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false) ) { String fieldName = queryBuilder.fieldName();
expectedQuery = new TermRangeQuery(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()), BytesRefs.toBytesRef(queryBuilder.from()), BytesRefs.toBytesRef(queryBuilder.to()),
queryBuilder.includeLower(), queryBuilder.includeUpper()); queryBuilder.includeLower(), queryBuilder.includeUpper());
} else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) { } else if (fieldName.equals(DATE_FIELD_NAME)) {
DateFieldMapper.Builder fieldMapperBuilder = new DateFieldMapper.Builder(queryBuilder.fieldName()); DateMathParser forcedDateParser = null;
DateFieldMapper fieldMapper = fieldMapperBuilder.build(new Mapper.BuilderContext(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(), new ContentPath())); if (queryBuilder.format() != null) {
DateMathParser dateMathParser = null; forcedDateParser = new DateMathParser(Joda.forPattern(queryBuilder.format()));
if (queryBuilder.format() != null) {
dateMathParser = new DateMathParser(Joda.forPattern(queryBuilder.format()), DateFieldMapper.Defaults.TIME_UNIT);
} }
DateTimeZone timeZone = null; DateTimeZone dateTimeZone = null;
if( queryBuilder.timeZone() != null) { if (queryBuilder.timeZone() != null) {
timeZone = DateTimeZone.forID(queryBuilder.timeZone()); dateTimeZone = DateTimeZone.forID(queryBuilder.timeZone());
} }
Long from = null; FieldMapper mapper = context.fieldMapper(queryBuilder.fieldName());
if (queryBuilder.from() != null) { expectedQuery = ((DateFieldMapper) mapper).fieldType().rangeQuery(queryBuilder.from(), queryBuilder.to(), queryBuilder.includeLower(), queryBuilder.includeUpper(), dateTimeZone, forcedDateParser, context);
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);
} else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) { } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) {
expectedQuery = NumericRangeQuery.newIntRange(INT_FIELD_NAME, (Integer) queryBuilder.from(), (Integer) queryBuilder.to(), queryBuilder.includeLower(), queryBuilder.includeUpper()); expectedQuery = NumericRangeQuery.newIntRange(INT_FIELD_NAME, (Integer) queryBuilder.from(), (Integer) queryBuilder.to(), queryBuilder.includeLower(), queryBuilder.includeUpper());
} else { } else {
@ -167,16 +162,6 @@ public class RangeQueryBuilderTest extends BaseQueryTestCase<RangeQueryBuilder>
query.toQuery(createContext()); 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 @Override
protected RangeQueryBuilder createEmptyQueryBuilder() { protected RangeQueryBuilder createEmptyQueryBuilder() {
return new RangeQueryBuilder(null); return new RangeQueryBuilder(null);