Skip to content

Commit

Permalink
Modify unsigned_long tests for broader ranges (#63782)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mayya-sharipova committed Oct 16, 2020
1 parent 2a20bd8 commit a11f2ae
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
Expand All @@ -38,6 +39,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.containsString;
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import static org.hamcrest.Matchers.equalTo;

@ESIntegTestCase.SuiteScopeTestCase

Expand Down Expand Up @@ -227,13 +229,13 @@ public void testAggs() {
{
SearchResponse response = client().prepareSearch("idx")
.setSize(0)
.addAggregation(histogram("ul_histo").field("ul_field").interval(9.223372036854776E18).minDocCount(0))
.addAggregation(histogram("ul_histo").field("ul_field").interval(9E18).minDocCount(0))
.get();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("ul_histo");

long[] expectedBucketDocCounts = { 3, 3, 4 };
double[] expectedBucketKeys = { 0, 9.223372036854776E18, 1.8446744073709552E19 };
double[] expectedBucketKeys = { 0, 9.0E18, 1.8E19 };
int i = 0;
for (Histogram.Bucket bucket : histo.getBuckets()) {
assertEquals(expectedBucketDocCounts[i], bucket.getDocCount());
Expand All @@ -247,20 +249,14 @@ public void testAggs() {
SearchResponse response = client().prepareSearch("idx")
.setSize(0)
.addAggregation(
range("ul_range").field("ul_field")
.addUnboundedTo(9.223372036854776E18)
.addRange(9.223372036854776E18, 1.8446744073709552E19)
.addUnboundedFrom(1.8446744073709552E19)
range("ul_range").field("ul_field").addUnboundedTo(9.0E18).addRange(9.0E18, 1.8E19).addUnboundedFrom(1.8E19)
)
.get();
assertSearchResponse(response);
Range range = response.getAggregations().get("ul_range");

long[] expectedBucketDocCounts = { 3, 3, 4 };
String[] expectedBucketKeys = {
"*-9.223372036854776E18",
"9.223372036854776E18-1.8446744073709552E19",
"1.8446744073709552E19-*" };
String[] expectedBucketKeys = { "*-9.0E18", "9.0E18-1.8E19", "1.8E19-*" };
int i = 0;
for (Range.Bucket bucket : range.getBuckets()) {
assertEquals(expectedBucketDocCounts[i], bucket.getDocCount());
Expand Down Expand Up @@ -293,4 +289,19 @@ public void testSortDifferentFormatsShouldFail() {
"Can't do sort across indices, as a field has [unsigned_long] type in one index, and different type in another index!"
);
}

public void testRangeQuery() {
SearchResponse response = client().prepareSearch("idx")
.setSize(0)
.setQuery(new RangeQueryBuilder("ul_field").to("9.0E18").includeUpper(false))
.get();
assertThat(response.getHits().getTotalHits().value, equalTo(3L));
response = client().prepareSearch("idx")
.setSize(0)
.setQuery(new RangeQueryBuilder("ul_field").from("9.0E18").to("1.8E19").includeUpper(false))
.get();
assertThat(response.getHits().getTotalHits().value, equalTo(3L));
response = client().prepareSearch("idx").setSize(0).setQuery(new RangeQueryBuilder("ul_field").from("1.8E19")).get();
assertThat(response.getHits().getTotalHits().value, equalTo(4L));
}
}

0 comments on commit a11f2ae

Please sign in to comment.