Skip to content

Commit 0cb48cf

Browse files
author
Christoph Büscher
committed
[Test] Re-Add integer_range and date_range field types for query builder tests (#28171)
The tests for those field types were removed in #26549 because the range mapper was moved to a module, but later this mapper was moved back to core in #27854. This change adds back those two field types like before to the general setup in AbstractQueryTestCase and adds some specifics to the RangeQueryBuilder and TermsQueryBuilder tests. Also adding back an integration test in SearchQueryIT that has been removed before but that can be kept with the mapper back in core now. Relates to #28147
1 parent 02a9287 commit 0cb48cf

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ protected RangeQueryBuilder doCreateTestQueryBuilder() {
6565
switch (randomIntBetween(0, 2)) {
6666
case 0:
6767
// use mapped integer field for numeric range queries
68-
query = new RangeQueryBuilder(INT_FIELD_NAME);
68+
query = new RangeQueryBuilder(randomBoolean() ? INT_FIELD_NAME : INT_RANGE_FIELD_NAME);
6969
query.from(randomIntBetween(1, 100));
7070
query.to(randomIntBetween(101, 200));
7171
break;
7272
case 1:
7373
// use mapped date field, using date string representation
74-
query = new RangeQueryBuilder(DATE_FIELD_NAME);
74+
query = new RangeQueryBuilder(randomBoolean() ? DATE_FIELD_NAME : DATE_RANGE_FIELD_NAME);
7575
query.from(new DateTime(System.currentTimeMillis() - randomIntBetween(0, 1000000), DateTimeZone.UTC).toString());
7676
query.to(new DateTime(System.currentTimeMillis() + randomIntBetween(0, 1000000), DateTimeZone.UTC).toString());
7777
// Create timestamp option only then we have a date mapper,
@@ -99,6 +99,10 @@ protected RangeQueryBuilder doCreateTestQueryBuilder() {
9999
if (randomBoolean()) {
100100
query.to(null);
101101
}
102+
if (query.fieldName().equals(INT_RANGE_FIELD_NAME) || query.fieldName().equals(DATE_RANGE_FIELD_NAME)) {
103+
query.relation(
104+
randomFrom(ShapeRelation.CONTAINS.toString(), ShapeRelation.INTERSECTS.toString(), ShapeRelation.WITHIN.toString()));
105+
}
102106
return query;
103107
}
104108

@@ -143,7 +147,9 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query,
143147

144148
} else if (getCurrentTypes().length == 0 ||
145149
(queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false
146-
&& queryBuilder.fieldName().equals(INT_FIELD_NAME) == false)) {
150+
&& queryBuilder.fieldName().equals(INT_FIELD_NAME) == false
151+
&& queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false
152+
&& queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false)) {
147153
assertThat(query, instanceOf(TermRangeQuery.class));
148154
TermRangeQuery termRangeQuery = (TermRangeQuery) query;
149155
assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName()));
@@ -219,6 +225,8 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query,
219225
maxInt--;
220226
}
221227
}
228+
} else if (queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) || queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME)) {
229+
// todo can't check RangeFieldQuery because its currently package private (this will change)
222230
} else {
223231
throw new UnsupportedOperationException();
224232
}

server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
package org.elasticsearch.index.query;
2121

22-
import org.apache.lucene.search.TermInSetQuery;
2322
import org.apache.lucene.search.BooleanQuery;
2423
import org.apache.lucene.search.ConstantScoreQuery;
2524
import org.apache.lucene.search.MatchNoDocsQuery;
2625
import org.apache.lucene.search.PointInSetQuery;
2726
import org.apache.lucene.search.Query;
27+
import org.apache.lucene.search.TermInSetQuery;
2828
import org.apache.lucene.util.BytesRef;
2929
import org.elasticsearch.ElasticsearchException;
3030
import org.elasticsearch.action.get.GetRequest;
@@ -77,9 +77,8 @@ protected TermsQueryBuilder doCreateTestQueryBuilder() {
7777
if (randomBoolean()) {
7878
// make between 0 and 5 different values of the same type
7979
String fieldName;
80-
do {
81-
fieldName = getRandomFieldName();
82-
} while (fieldName.equals(GEO_POINT_FIELD_NAME) || fieldName.equals(GEO_SHAPE_FIELD_NAME));
80+
fieldName = randomValueOtherThanMany(choice -> choice.equals(GEO_POINT_FIELD_NAME) || choice.equals(GEO_SHAPE_FIELD_NAME)
81+
|| choice.equals(INT_RANGE_FIELD_NAME) || choice.equals(DATE_RANGE_FIELD_NAME), () -> getRandomFieldName());
8382
Object[] values = new Object[randomInt(5)];
8483
for (int i = 0; i < values.length; i++) {
8584
values[i] = getRandomValueForFieldName(fieldName);

server/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
3737
import org.elasticsearch.index.query.Operator;
3838
import org.elasticsearch.index.query.QueryBuilders;
39+
import org.elasticsearch.index.query.RangeQueryBuilder;
3940
import org.elasticsearch.index.query.TermQueryBuilder;
4041
import org.elasticsearch.index.query.WrapperQueryBuilder;
4142
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
@@ -1893,4 +1894,17 @@ public void testQueryStringParserCache() throws Exception {
18931894
}
18941895
}
18951896

1897+
public void testRangeQueryRangeFields_24744() throws Exception {
1898+
assertAcked(prepareCreate("test").addMapping("type1", "int_range", "type=integer_range"));
1899+
1900+
client().prepareIndex("test", "type1", "1")
1901+
.setSource(jsonBuilder().startObject().startObject("int_range").field("gte", 10).field("lte", 20).endObject().endObject())
1902+
.get();
1903+
refresh();
1904+
1905+
RangeQueryBuilder range = new RangeQueryBuilder("int_range").relation("intersects").from(Integer.MIN_VALUE).to(Integer.MAX_VALUE);
1906+
SearchResponse searchResponse = client().prepareSearch("test").setQuery(range).get();
1907+
assertHitCount(searchResponse, 1);
1908+
}
1909+
18961910
}

test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,19 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
138138
public static final String STRING_FIELD_NAME = "mapped_string";
139139
protected static final String STRING_FIELD_NAME_2 = "mapped_string_2";
140140
protected static final String INT_FIELD_NAME = "mapped_int";
141+
protected static final String INT_RANGE_FIELD_NAME = "mapped_int_range";
141142
protected static final String DOUBLE_FIELD_NAME = "mapped_double";
142143
protected static final String BOOLEAN_FIELD_NAME = "mapped_boolean";
143144
protected static final String DATE_FIELD_NAME = "mapped_date";
145+
protected static final String DATE_RANGE_FIELD_NAME = "mapped_date_range";
144146
protected static final String OBJECT_FIELD_NAME = "mapped_object";
145147
protected static final String GEO_POINT_FIELD_NAME = "mapped_geo_point";
146148
protected static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape";
147-
protected static final String[] MAPPED_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME,
148-
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, OBJECT_FIELD_NAME, GEO_POINT_FIELD_NAME,
149+
protected static final String[] MAPPED_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME, INT_RANGE_FIELD_NAME,
150+
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, OBJECT_FIELD_NAME, GEO_POINT_FIELD_NAME,
149151
GEO_SHAPE_FIELD_NAME};
150-
private static final String[] MAPPED_LEAF_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME,
151-
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, GEO_POINT_FIELD_NAME, };
152+
private static final String[] MAPPED_LEAF_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME, INT_RANGE_FIELD_NAME,
153+
DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, GEO_POINT_FIELD_NAME, };
152154
private static final int NUMBER_OF_TESTQUERIES = 20;
153155

154156
protected static Version indexVersionCreated;
@@ -1077,9 +1079,11 @@ public void onRemoval(ShardId shardId, Accountable accountable) {
10771079
STRING_FIELD_NAME, "type=text",
10781080
STRING_FIELD_NAME_2, "type=keyword",
10791081
INT_FIELD_NAME, "type=integer",
1082+
INT_RANGE_FIELD_NAME, "type=integer_range",
10801083
DOUBLE_FIELD_NAME, "type=double",
10811084
BOOLEAN_FIELD_NAME, "type=boolean",
10821085
DATE_FIELD_NAME, "type=date",
1086+
DATE_RANGE_FIELD_NAME, "type=date_range",
10831087
OBJECT_FIELD_NAME, "type=object",
10841088
GEO_POINT_FIELD_NAME, "type=geo_point",
10851089
GEO_SHAPE_FIELD_NAME, "type=geo_shape"

0 commit comments

Comments
 (0)