Skip to content

Commit 4f94a2a

Browse files
authored
ESQL: Fix SearchStats#count(String) to count values not rows (#104891) (#104929)
SearchStats#count incorrectly counts the number of documents (or rows) in which a document appears instead of the actual number of values. This PR fixes this by looking at the term frequency instead of the doc count. Fix #104795
1 parent 2258610 commit 4f94a2a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

docs/changelog/104891.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 104891
2+
summary: "ESQL: Fix `SearchStats#count(String)` to count values not rows"
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 104795

x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,25 @@ ROW a = 1, c = null
919919
COUNT(c):long | a:integer
920920
0 | 1
921921
;
922+
923+
countMultiValuesRow
924+
ROW keyword_field = ["foo", "bar"], int_field = [1, 2, 3] | STATS ck = COUNT(keyword_field), ci = COUNT(int_field), c = COUNT(*);
925+
926+
ck:l | ci:l | c:l
927+
2 | 3 | 1
928+
;
929+
930+
931+
countSource
932+
FROM employees |
933+
STATS ck = COUNT(job_positions),
934+
cb = COUNT(is_rehired),
935+
cd = COUNT(salary_change),
936+
ci = COUNT(salary_change.int),
937+
c = COUNT(*),
938+
csv = COUNT(emp_no);
939+
940+
ck:l | cb:l | cd:l | ci:l | c:l | csv:l
941+
221 | 204 | 183 | 183 | 100 | 100
942+
;
943+

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/stats/SearchStats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ private static int countEntries(IndexReader indexReader, String field) {
248248
if (fieldInfo.getPointIndexDimensionCount() > 0) {
249249
PointValues points = reader.getPointValues(field);
250250
if (points != null) {
251-
count += points.getDocCount();
251+
count += points.size();
252252
}
253253
} else if (fieldInfo.getIndexOptions() != IndexOptions.NONE) {
254254
Terms terms = reader.terms(field);
255255
if (terms != null) {
256-
count += terms.getDocCount();
256+
count += terms.getSumTotalTermFreq();
257257
}
258258
} else {
259259
return -1; // no shortcut possible for fields that are not indexed

0 commit comments

Comments
 (0)