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.
This commit is contained in:
Tanguy Leroux 2017-05-30 09:39:41 +02:00 committed by GitHub
parent 628dabd663
commit 28d97df67c
4 changed files with 16 additions and 0 deletions

View File

@ -37,6 +37,7 @@ the statistics. The above request returns the following response:
... ...
"aggregations": { "aggregations": {
"matrixstats": { "matrixstats": {
"doc_count": 50,
"fields": [{ "fields": [{
"name": "income", "name": "income",
"count": 50, "count": 50,
@ -73,6 +74,8 @@ the statistics. The above request returns the following response:
} }
-------------------------------------------------- --------------------------------------------------
The `doc_count` field indicates the number of documents involved in the computation of the statistics.
==== Multi Value Fields ==== Multi Value Fields
The `matrix_stats` aggregation treats each document field as an independent sample. The `mode` parameter controls what The `matrix_stats` aggregation treats each document field as an independent sample. The `mode` parameter controls what

View File

@ -73,6 +73,9 @@ public class InternalMatrixStats extends InternalAggregation implements MatrixSt
/** get the number of documents */ /** get the number of documents */
@Override @Override
public long getDocCount() { public long getDocCount() {
if (stats == null) {
return 0;
}
return stats.docCount; return stats.docCount;
} }
@ -161,6 +164,7 @@ public class InternalMatrixStats extends InternalAggregation implements MatrixSt
@Override @Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(CommonFields.DOC_COUNT.getPreferredName(), getDocCount());
if (results != null && results.getFieldCounts().keySet().isEmpty() == false) { if (results != null && results.getFieldCounts().keySet().isEmpty() == false) {
builder.startArray(Fields.FIELDS); builder.startArray(Fields.FIELDS);
for (String fieldName : results.getFieldCounts().keySet()) { for (String fieldName : results.getFieldCounts().keySet()) {

View File

@ -133,6 +133,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } }
- match: {hits.total: 0} - match: {hits.total: 0}
- match: {aggregations.mfs.doc_count: 0}
--- ---
"Single value field": "Single value field":
@ -144,6 +145,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val3"]} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val3"]} } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 15}
- match: {aggregations.mfs.fields.0.count: 15} - match: {aggregations.mfs.fields.0.count: 15}
--- ---
@ -156,6 +158,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"]} } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 14}
- match: {aggregations.mfs.fields.0.count: 14} - match: {aggregations.mfs.fields.0.count: 14}
- match: {aggregations.mfs.fields.2.correlation.val2: 0.9569513137793205} - match: {aggregations.mfs.fields.2.correlation.val2: 0.9569513137793205}
@ -169,6 +172,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"], "missing" : {"val2" : 10} } } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "val3"], "missing" : {"val2" : 10} } } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 15}
- match: {aggregations.mfs.fields.0.count: 15} - match: {aggregations.mfs.fields.0.count: 15}
- match: {aggregations.mfs.fields.2.correlation.val2: 0.9567970467908384} - match: {aggregations.mfs.fields.2.correlation.val2: 0.9567970467908384}

View File

@ -133,6 +133,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }
- match: {hits.total: 0} - match: {hits.total: 0}
- match: {aggregations.mfs.doc_count: 0}
--- ---
"Multi value field Max": "Multi value field Max":
@ -144,6 +145,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "max"} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "max"} } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 14}
- match: {aggregations.mfs.fields.0.count: 14} - match: {aggregations.mfs.fields.0.count: 14}
- match: {aggregations.mfs.fields.0.correlation.val1: 0.06838646533369998} - match: {aggregations.mfs.fields.0.correlation.val1: 0.06838646533369998}
@ -157,6 +159,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "min"} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "min"} } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 14}
- match: {aggregations.mfs.fields.0.count: 14} - match: {aggregations.mfs.fields.0.count: 14}
- match: {aggregations.mfs.fields.0.correlation.val1: -0.09777682707831963} - match: {aggregations.mfs.fields.0.correlation.val1: -0.09777682707831963}
@ -170,6 +173,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 13}
- match: {aggregations.mfs.fields.0.count: 13} - match: {aggregations.mfs.fields.0.count: 13}
- match: {aggregations.mfs.fields.0.correlation.val1: -0.044997535185684244} - match: {aggregations.mfs.fields.0.correlation.val1: -0.044997535185684244}
@ -183,6 +187,7 @@ setup:
body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"], "missing" : {"val2" : 10, "vals" : 5 } } } } } body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"], "missing" : {"val2" : 10, "vals" : 5 } } } } }
- match: {hits.total: 15} - match: {hits.total: 15}
- match: {aggregations.mfs.doc_count: 15}
- match: {aggregations.mfs.fields.0.count: 15} - match: {aggregations.mfs.fields.0.count: 15}
- match: {aggregations.mfs.fields.0.correlation.val2: 0.04028024709708195} - match: {aggregations.mfs.fields.0.correlation.val2: 0.04028024709708195}