Skip to content

Commit

Permalink
[8.0] fix: missing fields in range aggregation response for date fiel…
Browse files Browse the repository at this point in the history
…ds (elastic#82732) (elastic#82755)

When running a range aggregation on a date field both `fromAsStr` and
`toAsStr` need to be parsed to extract the correct range boundaries.
Not doing so results in +/- infinite values in `to`, `from`, `originalTo`
and `originalFrom` which in turn result in:
* incorrect key values (wildcard character used for infinite values);
* missing fields in the response (to, from, to_as_string, from_as_string)
  due to checks for initite values in the serialization logic;

* Update yaml test skip version after back-port from 8.1.0
  • Loading branch information
salvatore-campagna authored Jan 18, 2022
1 parent 222cded commit 12a0571
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ setup:
half_float:
type: half_float

- do:
indices.create:
index: date_range_test
body:
settings:
number_of_replicas: 0
mappings:
properties:
date:
type: date
format: strict_date_time||strict_date

- do:
cluster.health:
wait_for_status: yellow
Expand All @@ -35,13 +47,30 @@ setup:
- {"index": {}}
- {}

- do:
bulk:
index: date_range_test
refresh: true
body:
- { "index": { } }
- { "date": "2021-05-01T07:10:00Z" }
- { "index": { } }
- { "date": "2021-05-02T08:34:00Z" }
- { "index": { } }
- { "date": "2021-05-03T08:36:00Z" }
- { "index": { } }
- { "date": "2021-05-04T09:05:00Z" }
- { "index": { } }
- { "date": "2021-05-06T09:22:00Z" }

---
"Float Endpoint Exclusive":
- skip:
version: " - 7.15.99"
reason: Bug fixed in 7.16.0
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -70,6 +99,7 @@ setup:
reason: Bug fixed in 7.16.0
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -98,6 +128,7 @@ setup:
reason: "AwaitsFix https://github.com/elastic/elasticsearch/issues/82476"
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -136,6 +167,7 @@ setup:
reason: Bug fixed in 8.1.0 and backported to 8.0.0
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -171,6 +203,7 @@ setup:
"Double range no decimal":
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -206,6 +239,7 @@ setup:
"Double range with missing value":
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -242,6 +276,7 @@ setup:
"Null to and from":
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -279,6 +314,7 @@ setup:
"Range agg on long field":
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -314,6 +350,7 @@ setup:
"Double range default keyed response":
- do:
search:
index: test
body:
size: 0
aggs:
Expand Down Expand Up @@ -345,3 +382,30 @@ setup:
- match: { aggregations.double_range.buckets.last.from: 150.0 }
- is_false: aggregations.double_range.buckets.last.to
- match: { aggregations.double_range.buckets.last.doc_count: 0 }

---
"Range aggregation on date field":
- skip:
version: " - 7.16.99"
reason: Fixed in 8.1.0 and backported to 8.0.0"

- do:
search:
index: date_range_test
body:
size: 0
aggs:
date_range:
range:
field: date
ranges:
{ from: 2021-05-01T00:00:00Z, to: 2021-05-05T00:00:00Z }

- match: { hits.total.value: 5 }
- length: { aggregations.date_range.buckets: 1 }
- match: { aggregations.date_range.buckets.0.doc_count: 4 }
- match: { aggregations.date_range.buckets.0.key: "2021-05-01T00:00:00.000Z-2021-05-05T00:00:00.000Z" }
- match: { aggregations.date_range.buckets.0.from: 1619827200000 }
- match: { aggregations.date_range.buckets.0.from_as_string: "2021-05-01T00:00:00.000Z" }
- match: { aggregations.date_range.buckets.0.to: 1620172800000 }
- match: { aggregations.date_range.buckets.0.to_as_string: "2021-05-05T00:00:00.000Z" }
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ protected RangeAggregatorFactory innerBuild(
if (range.toAsStr != null) {
to = parser.parseDouble(range.toAsStr, false, context::nowInMillis);
}
String key = range.key != null ? range.key : generateKey(range.originalFrom, range.originalTo, config.format());
return new Range(key, from, range.from, range.fromAsStr, to, range.to, range.toAsStr);
double originalFrom = range.fromAsStr != null ? from : range.from;
double originalTo = range.toAsStr != null ? to : range.to;
String key = range.key != null ? range.key : generateKey(originalFrom, originalTo, config.format());
return new Range(key, from, originalFrom, range.fromAsStr, to, originalTo, range.toAsStr);
});
if (ranges.length == 0) {
throw new IllegalArgumentException("No [ranges] specified for the [" + this.getName() + "] aggregation");
Expand Down

0 comments on commit 12a0571

Please sign in to comment.