Skip to content

Commit

Permalink
Fix cardinality agg for const_keyword (elastic#99814)
Browse files Browse the repository at this point in the history
* Fix cardinality agg for const_keyword

const_keyword fields don't show up in the leafReader, since they have
a const value. elastic#92060 modified the logic to return no results in case
the leaf reader contains no information about the requested field in a
cardinality aggregation. This is wrong for const_keyword fields, as they
contain up to 1 distinct value.

To fix this, we fall back to the old logic in this case that can
handle const_keyword fields properly.

Fixes elastic#99776

* Update docs/changelog/99814.yaml

* Update skip ranges for broken releases.
  • Loading branch information
kkrik-es authored Sep 25, 2023
1 parent 58268ed commit 923a944
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/99814.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 99814
summary: Fix cardinality agg for `const_keyword`
area: Aggregations
type: bug
issues:
- 99776
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,14 @@ public CompetitiveIterator competitiveIterator() {
}
} else {
final FieldInfo fi = aggCtx.getLeafReaderContext().reader().getFieldInfos().fieldInfo(field);
if (fi == null) {
// The field doesn't exist at all, we can skip the segment entirely
noData++;
return LeafBucketCollector.NO_OP_COLLECTOR;
} else if (fi.getIndexOptions() != IndexOptions.NONE) {
if (fi != null && fi.getIndexOptions() != IndexOptions.NONE) {
// The field doesn't have terms while index options are not NONE. This means that this segment doesn't have a single
// value for the field.
noData++;
return LeafBucketCollector.NO_OP_COLLECTOR;
}
// Otherwise we might be aggregating e.g. an IP field, which indexes data using points rather than an inverted index.
// Otherwise we might be aggregating e.g. an IP or a const_keyword field, which index data using points rather than an
// inverted index.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,40 @@ setup:
- match: { aggregations.agg.buckets.1.0-bucket.doc_count: 0 }
- match: { aggregations.agg.buckets.2.key: 200.0 }
- match: { aggregations.agg.buckets.2.0-bucket.doc_count: 0 }


---
Cardinality agg:
- skip:
version: " - 7.6.99, 8.9.00 - 8.10.99"
reason: "constant_keyword was added in 7.7, bug introduced in 8.9 and fixed in 8.11"

- do:
indices.create:
index: test3
body:
mappings:
properties:
test:
type: constant_keyword
value: value1

- do:
bulk:
index: test3
refresh: true
body: |
{"index":{}}
{ "test": "value1" }
- do:
search:
index: test3
body:
size: 0
aggs:
card:
cardinality:
field: test

- match: { aggregations.card.value: 1 }

0 comments on commit 923a944

Please sign in to comment.