Skip to content

Commit 28d97df

Browse files
authored
Add document count to Matrix Stats aggregation response (#24776)
This commit adds a `doc_count` field to the response body of Matrix Stats aggregation. It exposes the number of documents involved in the computation of statistics, a value that can already be retrieved using the method MatrixStats.getDocCount() in the Java API.
1 parent 628dabd commit 28d97df

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

docs/reference/aggregations/matrix/stats-aggregation.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ the statistics. The above request returns the following response:
3737
...
3838
"aggregations": {
3939
"matrixstats": {
40+
"doc_count": 50,
4041
"fields": [{
4142
"name": "income",
4243
"count": 50,
@@ -73,6 +74,8 @@ the statistics. The above request returns the following response:
7374
}
7475
--------------------------------------------------
7576

77+
The `doc_count` field indicates the number of documents involved in the computation of the statistics.
78+
7679
==== Multi Value Fields
7780

7881
The `matrix_stats` aggregation treats each document field as an independent sample. The `mode` parameter controls what

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/matrix/stats/InternalMatrixStats.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public String getWriteableName() {
7373
/** get the number of documents */
7474
@Override
7575
public long getDocCount() {
76+
if (stats == null) {
77+
return 0;
78+
}
7679
return stats.docCount;
7780
}
7881

@@ -161,6 +164,7 @@ static class Fields {
161164

162165
@Override
163166
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
167+
builder.field(CommonFields.DOC_COUNT.getPreferredName(), getDocCount());
164168
if (results != null && results.getFieldCounts().keySet().isEmpty() == false) {
165169
builder.startArray(Fields.FIELDS);
166170
for (String fieldName : results.getFieldCounts().keySet()) {

modules/aggs-matrix-stats/src/test/resources/rest-api-spec/test/stats/30_single_value_field.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ setup:
133133
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } }
134134

135135
- match: {hits.total: 0}
136+
- match: {aggregations.mfs.doc_count: 0}
136137

137138
---
138139
"Single value field":
@@ -144,6 +145,7 @@ setup:
144145
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val3"]} } } }
145146

146147
- match: {hits.total: 15}
148+
- match: {aggregations.mfs.doc_count: 15}
147149
- match: {aggregations.mfs.fields.0.count: 15}
148150

149151
---
@@ -156,6 +158,7 @@ setup:
156158
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } }
157159

158160
- match: {hits.total: 15}
161+
- match: {aggregations.mfs.doc_count: 14}
159162
- match: {aggregations.mfs.fields.0.count: 14}
160163
- match: {aggregations.mfs.fields.2.correlation.val2: 0.9569513137793205}
161164

@@ -169,6 +172,7 @@ setup:
169172
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"], "missing" : {"val2" : 10} } } } }
170173

171174
- match: {hits.total: 15}
175+
- match: {aggregations.mfs.doc_count: 15}
172176
- match: {aggregations.mfs.fields.0.count: 15}
173177
- match: {aggregations.mfs.fields.2.correlation.val2: 0.9567970467908384}
174178

modules/aggs-matrix-stats/src/test/resources/rest-api-spec/test/stats/40_multi_value_field.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ setup:
133133
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }
134134

135135
- match: {hits.total: 0}
136+
- match: {aggregations.mfs.doc_count: 0}
136137

137138
---
138139
"Multi value field Max":
@@ -144,6 +145,7 @@ setup:
144145
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "max"} } } }
145146

146147
- match: {hits.total: 15}
148+
- match: {aggregations.mfs.doc_count: 14}
147149
- match: {aggregations.mfs.fields.0.count: 14}
148150
- match: {aggregations.mfs.fields.0.correlation.val1: 0.06838646533369998}
149151

@@ -157,6 +159,7 @@ setup:
157159
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "min"} } } }
158160

159161
- match: {hits.total: 15}
162+
- match: {aggregations.mfs.doc_count: 14}
160163
- match: {aggregations.mfs.fields.0.count: 14}
161164
- match: {aggregations.mfs.fields.0.correlation.val1: -0.09777682707831963}
162165

@@ -170,6 +173,7 @@ setup:
170173
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }
171174

172175
- match: {hits.total: 15}
176+
- match: {aggregations.mfs.doc_count: 13}
173177
- match: {aggregations.mfs.fields.0.count: 13}
174178
- match: {aggregations.mfs.fields.0.correlation.val1: -0.044997535185684244}
175179

@@ -183,6 +187,7 @@ setup:
183187
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"], "missing" : {"val2" : 10, "vals" : 5 } } } } }
184188

185189
- match: {hits.total: 15}
190+
- match: {aggregations.mfs.doc_count: 15}
186191
- match: {aggregations.mfs.fields.0.count: 15}
187192
- match: {aggregations.mfs.fields.0.correlation.val2: 0.04028024709708195}
188193

0 commit comments

Comments
 (0)