From 923a9442141552d0896cbb7e42b4202b1da8eec6 Mon Sep 17 00:00:00 2001 From: Kostas Krikellas <131142368+kkrik-es@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:14:56 +0300 Subject: [PATCH] Fix cardinality agg for const_keyword (#99814) * Fix cardinality agg for const_keyword const_keyword fields don't show up in the leafReader, since they have a const value. #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 #99776 * Update docs/changelog/99814.yaml * Update skip ranges for broken releases. --- docs/changelog/99814.yaml | 6 +++ .../GlobalOrdCardinalityAggregator.java | 9 ++--- .../test/constant_keyword/10_basic.yml | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 docs/changelog/99814.yaml diff --git a/docs/changelog/99814.yaml b/docs/changelog/99814.yaml new file mode 100644 index 0000000000000..1632be42b4e4c --- /dev/null +++ b/docs/changelog/99814.yaml @@ -0,0 +1,6 @@ +pr: 99814 +summary: Fix cardinality agg for `const_keyword` +area: Aggregations +type: bug +issues: + - 99776 diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GlobalOrdCardinalityAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GlobalOrdCardinalityAggregator.java index ee412666a21fa..5e59da1591270 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GlobalOrdCardinalityAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GlobalOrdCardinalityAggregator.java @@ -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. } } diff --git a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/constant_keyword/10_basic.yml b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/constant_keyword/10_basic.yml index 3c5f29178881d..a89b24ff45593 100644 --- a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/constant_keyword/10_basic.yml +++ b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/constant_keyword/10_basic.yml @@ -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 }