Skip to content

Commit a11f2ae

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 2a20bd8 commit a11f2ae

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

Lines changed: 21 additions & 10 deletions
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

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

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

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

0 commit comments

Comments
 (0)