Skip to content

Commit a43b5a5

Browse files
Modify unsigned_long tests for broader ranges (#63782)
UnsignedLongTests for the range agg was using very specific intervals that double type can not distinguish due to lack of precision: 9.223372036854776000E18 == 9.223372036854775807E18 returns true If we add the corresponding range query test, it will return different number of hits than the range agg, as range query unlike range agg doesn't convert valued to double type, and hence more precise. This patch make broader ranges for the range agg test (so values converted to doubles don't loose precision), and hence corresponding range query will return the same number of hits. Relates to #60050
1 parent 2eae9b6 commit a43b5a5

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

x-pack/plugin/mapper-unsigned-long/src/test/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongTests.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.support.WriteRequest;
1616
import org.elasticsearch.common.settings.Settings;
1717
import org.elasticsearch.index.query.QueryBuilders;
18+
import org.elasticsearch.index.query.RangeQueryBuilder;
1819
import org.elasticsearch.plugins.Plugin;
1920
import org.elasticsearch.search.SearchHit;
2021
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
@@ -38,6 +39,7 @@
3839
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
3940
import static org.hamcrest.Matchers.containsString;
4041
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
42+
import static org.hamcrest.Matchers.equalTo;
4143

4244
@ESIntegTestCase.SuiteScopeTestCase
4345

@@ -229,13 +231,13 @@ public void testAggs() {
229231
{
230232
SearchResponse response = client().prepareSearch("idx")
231233
.setSize(0)
232-
.addAggregation(histogram("ul_histo").field("ul_field").interval(9.223372036854776E18).minDocCount(0))
234+
.addAggregation(histogram("ul_histo").field("ul_field").interval(9E18).minDocCount(0))
233235
.get();
234236
assertSearchResponse(response);
235237
Histogram histo = response.getAggregations().get("ul_histo");
236238

237239
long[] expectedBucketDocCounts = { 3, 3, 4 };
238-
double[] expectedBucketKeys = { 0, 9.223372036854776E18, 1.8446744073709552E19 };
240+
double[] expectedBucketKeys = { 0, 9.0E18, 1.8E19 };
239241
int i = 0;
240242
for (Histogram.Bucket bucket : histo.getBuckets()) {
241243
assertEquals(expectedBucketDocCounts[i], bucket.getDocCount());
@@ -249,20 +251,14 @@ public void testAggs() {
249251
SearchResponse response = client().prepareSearch("idx")
250252
.setSize(0)
251253
.addAggregation(
252-
range("ul_range").field("ul_field")
253-
.addUnboundedTo(9.223372036854776E18)
254-
.addRange(9.223372036854776E18, 1.8446744073709552E19)
255-
.addUnboundedFrom(1.8446744073709552E19)
254+
range("ul_range").field("ul_field").addUnboundedTo(9.0E18).addRange(9.0E18, 1.8E19).addUnboundedFrom(1.8E19)
256255
)
257256
.get();
258257
assertSearchResponse(response);
259258
Range range = response.getAggregations().get("ul_range");
260259

261260
long[] expectedBucketDocCounts = { 3, 3, 4 };
262-
String[] expectedBucketKeys = {
263-
"*-9.223372036854776E18",
264-
"9.223372036854776E18-1.8446744073709552E19",
265-
"1.8446744073709552E19-*" };
261+
String[] expectedBucketKeys = { "*-9.0E18", "9.0E18-1.8E19", "1.8E19-*" };
266262
int i = 0;
267263
for (Range.Bucket bucket : range.getBuckets()) {
268264
assertEquals(expectedBucketDocCounts[i], bucket.getDocCount());
@@ -295,4 +291,19 @@ public void testSortDifferentFormatsShouldFail() {
295291
"Can't do sort across indices, as a field has [unsigned_long] type in one index, and different type in another index!"
296292
);
297293
}
294+
295+
public void testRangeQuery() {
296+
SearchResponse response = client().prepareSearch("idx")
297+
.setSize(0)
298+
.setQuery(new RangeQueryBuilder("ul_field").to("9.0E18").includeUpper(false))
299+
.get();
300+
assertThat(response.getHits().getTotalHits().value, equalTo(3L));
301+
response = client().prepareSearch("idx")
302+
.setSize(0)
303+
.setQuery(new RangeQueryBuilder("ul_field").from("9.0E18").to("1.8E19").includeUpper(false))
304+
.get();
305+
assertThat(response.getHits().getTotalHits().value, equalTo(3L));
306+
response = client().prepareSearch("idx").setSize(0).setQuery(new RangeQueryBuilder("ul_field").from("1.8E19")).get();
307+
assertThat(response.getHits().getTotalHits().value, equalTo(4L));
308+
}
298309
}

0 commit comments

Comments
 (0)