Fix RangeFieldMapper rangeQuery to properly handle relations

This commit fixes the RangeFieldMapper and RangeQueryBuilder to pass the correct relation to the RangeQuery when performing a range query over range fields.
This commit is contained in:
Nicholas Knize 2017-05-19 15:00:48 -05:00
parent c584c2fd71
commit 5ae48f7dd5
4 changed files with 22 additions and 11 deletions

View File

@ -282,12 +282,6 @@ public class RangeFieldMapper extends FieldMapper {
return query; return query;
} }
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper,
QueryShardContext context) {
return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, ShapeRelation.INTERSECTS, context);
}
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper,
ShapeRelation relation, QueryShardContext context) { ShapeRelation relation, QueryShardContext context) {
failIfNotIndexed(); failIfNotIndexed();

View File

@ -495,9 +495,9 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, query = ((DateFieldMapper.DateFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper,
timeZone, getForceDateParser(), context); timeZone, getForceDateParser(), context);
} else if (mapper instanceof RangeFieldMapper.RangeFieldType && mapper.typeName() == RangeFieldMapper.RangeType.DATE.name) { } else if (mapper instanceof RangeFieldMapper.RangeFieldType) {
DateMathParser forcedDateParser = null; DateMathParser forcedDateParser = null;
if (this.format != null) { if (mapper.typeName() == RangeFieldMapper.RangeType.DATE.name && this.format != null) {
forcedDateParser = new DateMathParser(this.format); forcedDateParser = new DateMathParser(this.format);
} }
query = ((RangeFieldMapper.RangeFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper, query = ((RangeFieldMapper.RangeFieldType) mapper).rangeQuery(from, to, includeLower, includeUpper,

View File

@ -79,9 +79,6 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
query.format("yyyy-MM-dd'T'HH:mm:ss.SSSZZ"); query.format("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
} }
} }
if (query.fieldName().equals(DATE_RANGE_FIELD_NAME)) {
query.relation(RandomPicks.randomFrom(random(), ShapeRelation.values()).getRelationName());
}
break; break;
case 2: case 2:
default: default:
@ -97,6 +94,9 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
if (randomBoolean()) { if (randomBoolean()) {
query.to(null); query.to(null);
} }
if (query.fieldName().equals(INT_RANGE_FIELD_NAME) || query.fieldName().equals(DATE_RANGE_FIELD_NAME)) {
query.relation(RandomPicks.randomFrom(random(), ShapeRelation.values()).getRelationName());
}
return query; return query;
} }

View File

@ -34,6 +34,7 @@ import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WrapperQueryBuilder; import org.elasticsearch.index.query.WrapperQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
@ -1843,4 +1844,20 @@ public class SearchQueryIT extends ESIntegTestCase {
assertThat(i + " expected: " + first + " actual: " + actual, Float.compare(first, actual), equalTo(0)); assertThat(i + " expected: " + first + " actual: " + actual, Float.compare(first, actual), equalTo(0));
} }
} }
public void testRangeQueryRangeFields_24744() throws Exception {
assertAcked(prepareCreate("test")
.addMapping("type1", "int_range", "type=integer_range"));
client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder()
.startObject()
.startObject("int_range").field("gte", 10).field("lte", 20).endObject()
.endObject()).get();
refresh();
RangeQueryBuilder range = new RangeQueryBuilder("int_range").relation("intersects").from(Integer.MIN_VALUE).to(Integer.MAX_VALUE);
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
assertHitCount(searchResponse, 1);
}
} }