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);
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);

View File

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

View File

@ -24,13 +24,16 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.joda.DateMathParser;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.ContentPath;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.mapper.core.DateFieldMapper.DateFieldType;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
@ -94,31 +97,23 @@ public class RangeQueryBuilderTest extends BaseQueryTestCase<RangeQueryBuilder>
@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<RangeQueryBuilder>
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);