使用与帖子here相同的示例。假设两个字段address.city
和address.stateOrProvince
都是嵌套类型,并且聚合不指定"nested":{"path":<nested path>}
。聚合结果没有doc_count的总和。由于未指定"nested":{"path":<nested path>}
,因此根级别命中的根级别doc_count不是存储桶中doc_count的总和。
[编辑],将示例添加为Val的注释,activities
是嵌套类型。 query1指定了"nested":{"path":<nested path>"}
但查询2没有。在result2中,如何获取存储桶中所有项目的doc_count的总和,在这种情况下为2 + 1 + 1 + 1。
// DOC1
{
"activities.entry.time": "2015-09-15T17:49:35.687Z",
"activities.duration": "18747",
"activities.entry.time": "2015-09-15T17:49:54.639Z",
"activities.duration": "65",
"activities.entry.time": "2015-09-15T17:49:56.737Z",
"activities.duration": "449",
"activities.entry.time": "2015-09-15T17:49:56.772Z",
"activities.duration": "412"}
// DOC2
{
"activities.entry.time": "2015-09-15T17:55:43.304Z",
"activities.duration": "18675",
"activities.entry.time": "2015-09-15T17:56:02.150Z",
"activities.duration": "88",
"activities.entry.time": "2015-09-15T17:56:04.269Z",
"activities.duration": "396",
"activities.entry.time": "2015-09-15T17:56:04.314Z",
"activities.duration": "349"}
// doc3的
{
"activities.entry.time": "2015-09-15T17:49:56.087Z",
"activities.duration": "18723",
"activities.entry.time": "2015-09-15T17:50:15.002Z",
"activities.duration": "77",
"activities.entry.time": "2015-09-15T17:50:17.125Z",
"activities.duration": "418",
"activities.entry.time": "2015-09-15T17:50:17.137Z",
"activities.duration": "404"}
// QUERY1
{
"size": 0,
"aggs": {
"dim0": {
"nested": {
"path": "activities"
},
"aggs": {
"dh": {
"date_histogram": {
"field": "activities.entry.time",
"interval": "minute",
"format": "yyyy-MM-dd",
"pre_zone": -7
},
"aggs": {
"avg_duration": {
"avg": {
"field": "activities.duration"
}
}
}
}
}
}
}}
// RESULT1
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"dim0": {
"doc_count": 12,
"dh": {
"buckets": [{
"key_as_string": "2015-09-15",
"key": 1442339340000, //2015-09-15T17:49:00.000Z
"doc_count": 5, //4 count in doc1 + 1 count in doc3
"avg_duration": {
"value": 7679.2 //(18747+65+449+412+18723) / 5
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339400000, //2015-09-15T17:50:00.000Z
"doc_count": 3,
"avg_duration": {
"value": 299.6666666666667
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339700000, //2015-09-15T17:55:00.000Z
"doc_count": 1,
"avg_duration": {
"value": 18675.0
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339760000, //2015-09-15T17:56:00.000Z
"doc_count": 3,
"avg_duration": {
"value": 277.6666666666667
}
}]
}
}
}}
// QUERY2
{
"size": 0,
"aggs": {
"dh": {
"date_histogram": {
"field": "activities.entry.time",
"interval": "minute",
"format": "yyyy-MM-dd",
"pre_zone": -7
},
"aggs": {
"avg_duration": {
"avg": {
"field": "activities.duration"
}
}
}
}
}}
// RESULT2
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"dh": {
"buckets": [{
"key_as_string": "2015-09-15",
"key": 1442339340000, //2015-09-15T17:49:00.000Z
"doc_count": 2, //doc1 + doc3
"avg_duration": {
"value": 4911.875 //(18747+65+449+412)+(18723+77+418+404)/8
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339400000, //2015-09-15T17:50:00.000Z
"doc_count": 1,
"avg_duration": {
"value": 4905.5
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339700000, //2015-09-15T17:55:00.000Z
"doc_count": 1,
"avg_duration": {
"value": 4877.0
}
}, {
"key_as_string": "2015-09-15",
"key": 1442339760000, //2015-09-15T17:56:00.000Z
"doc_count": 1,
"avg_duration": {
"value": 4877.0
}
}]
}
}}