|
23 | 23 |
|
24 | 24 | import org.apache.lucene.document.IntPoint; |
25 | 25 | import org.apache.lucene.document.LongPoint; |
| 26 | +import org.apache.lucene.index.Term; |
| 27 | +import org.apache.lucene.search.ConstantScoreQuery; |
26 | 28 | import org.apache.lucene.search.IndexOrDocValuesQuery; |
| 29 | +import org.apache.lucene.search.MatchNoDocsQuery; |
27 | 30 | import org.apache.lucene.search.PointRangeQuery; |
28 | 31 | import org.apache.lucene.search.Query; |
| 32 | +import org.apache.lucene.search.TermQuery; |
29 | 33 | import org.apache.lucene.search.TermRangeQuery; |
30 | 34 | import org.elasticsearch.ElasticsearchParseException; |
31 | 35 | import org.elasticsearch.common.ParsingException; |
32 | 36 | import org.elasticsearch.common.geo.ShapeRelation; |
33 | 37 | import org.elasticsearch.common.lucene.BytesRefs; |
34 | 38 | import org.elasticsearch.index.mapper.DateFieldMapper; |
| 39 | +import org.elasticsearch.index.mapper.FieldNamesFieldMapper; |
35 | 40 | import org.elasticsearch.index.mapper.MappedFieldType; |
36 | 41 | import org.elasticsearch.index.mapper.MappedFieldType.Relation; |
37 | 42 | import org.elasticsearch.index.mapper.MapperService; |
|
52 | 57 | import static org.hamcrest.Matchers.sameInstance; |
53 | 58 |
|
54 | 59 | public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuilder> { |
55 | | - |
56 | 60 | @Override |
57 | 61 | protected RangeQueryBuilder doCreateTestQueryBuilder() { |
58 | 62 | RangeQueryBuilder query; |
@@ -122,7 +126,16 @@ protected Map<String, RangeQueryBuilder> getAlternateVersions() { |
122 | 126 |
|
123 | 127 | @Override |
124 | 128 | protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { |
125 | | - if (getCurrentTypes().length == 0 || |
| 129 | + if (queryBuilder.from() == null && queryBuilder.to() == null) { |
| 130 | + final Query expectedQuery; |
| 131 | + if (getCurrentTypes().length > 0) { |
| 132 | + expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, queryBuilder.fieldName()))); |
| 133 | + } else { |
| 134 | + expectedQuery = new MatchNoDocsQuery("no mappings yet"); |
| 135 | + } |
| 136 | + assertThat(query, equalTo(expectedQuery)); |
| 137 | + |
| 138 | + } else if (getCurrentTypes().length == 0 || |
126 | 139 | (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false |
127 | 140 | && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false |
128 | 141 | && queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false |
@@ -425,6 +438,16 @@ protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteC |
425 | 438 | assertThat(rewrittenRange.fieldName(), equalTo(fieldName)); |
426 | 439 | assertThat(rewrittenRange.from(), equalTo(null)); |
427 | 440 | assertThat(rewrittenRange.to(), equalTo(null)); |
| 441 | + |
| 442 | + // Range query with open bounds rewrite to an exists query |
| 443 | + final Query luceneQuery = rewrittenRange.toQuery(queryShardContext); |
| 444 | + final Query expectedQuery; |
| 445 | + if (getCurrentTypes().length > 0) { |
| 446 | + expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, query.fieldName()))); |
| 447 | + } else { |
| 448 | + expectedQuery = new MatchNoDocsQuery("no mappings yet"); |
| 449 | + } |
| 450 | + assertThat(luceneQuery, equalTo(expectedQuery)); |
428 | 451 | } |
429 | 452 |
|
430 | 453 | public void testRewriteDateToMatchAllWithTimezoneAndFormat() throws IOException { |
|
0 commit comments