我在过滤聚合结果后遇到问题。我认为我走在正确的轨道上,但我觉得我正在追逐我的尾巴。ElasticSearch - 筛选器嵌套聚合
下面是它的外观:
PUT /my_index
{
"mappings": {
"reporting": {
"properties": {
"events": {
"type": "nested",
"properties": {
"name": { "type": "string", "index" : "not_analyzed" },
"date": { "type": "date" }
}
}
}
}
}
}
所以,我的文件是这样的:
{
"events": [
{ "name": "INSTALL", "date": "2014-11-01" },
{ "name": "UNINSTALL", "date": "2014-11-03" },
{ "name": "INSTALL", "date": "2014-11-04" },
...
]
}
现在,当我的索引的一些数据,例如:
PUT /my_index/reporting/1
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
},
{
"name": "UNINSTALL",
"date": "2014-11-05"
}
]
}
PUT /my_index/reporting/2
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
},
{
"name": "UNINSTALL",
"date": "2014-11-03"
}
]
}
PUT /my_index/reporting/3
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
},
{
"name": "UNINSTALL",
"date": "2014-11-02"
}
]
}
PUT /my_index/reporting/4
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
},
{
"name": "UNINSTALL",
"date": "2014-11-02"
},
{
"name": "INSTALL",
"date": "2014-11-03"
}
]
}
PUT /my_index/reporting/5
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
},
{
"name": "UNINSTALL",
"date": "2014-11-03"
},
{
"name": "INSTALL",
"date": "2014-11-03"
}
]
}
PUT /my_index/reporting/6
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-03"
},
{
"name": "UNINSTALL",
"date": "2014-11-03"
},
{
"name": "INSTALL",
"date": "2014-11-05"
}
]
}
PUT /my_index/reporting/7
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-02"
},
{
"name": "UNINSTALL",
"date": "2014-11-03"
},
{
"name": "INSTALL",
"date": "2014-11-05"
}
]
}
PUT /my_index/reporting/8
{
"events": [
{
"name": "INSTALL",
"date": "2014-11-01"
}
]
}
我想要得到安装后(包括)2014-11-02和没有卸载的人数(所以,UNINSTALL在2014年11月2日之前或没有U NINSTALL事件),并将它们分组在date_histogram含义中(具有“日期” - >“计数”数据的桶)。
我设法在这个嵌套数据上写入过滤器,所以我可以得到过滤结果,但是当涉及到直方图聚合时,我总是追着我的尾巴。
这是我卡住的地方。
GET /my_index/reporting/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "events",
"filter": {
"bool": {
"must": [
{
"term": {
"name": "INSTALL"
}
},
{
"range": {
"date": {
"gte": "2014-11-02"
}
}
}
]
}
}
}
},
{
"nested": {
"path": "events",
"filter": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"term": {
"name": "UNINSTALL"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"name": "UNINSTALL"
}
},
{
"range": {
"date": {
"lt": "2014-11-02"
}
}
}
]
}
}
]
}
}
}
}
]
}
}
}
},
"aggregations": {
"filtered_result": {
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "events",
"filter": {
"bool": {
"must": [
{
"term": {
"name": "INSTALL"
}
},
{
"range": {
"date": {
"gte": "2014-11-02"
}
}
}
]
}
}
}
},
{
"nested": {
"path": "events",
"filter": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"term": {
"name": "UNINSTALL"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"name": "UNINSTALL"
}
},
{
"range": {
"date": {
"lt": "2014-11-02"
}
}
}
]
}
}
]
}
}
}
}
]
}
},
"aggs": {
"result": {
"nested": {
"path": "events"
},
"aggs": {
"NAME": {
"terms": {
"field": "events.date",
"format": "yyyy-MM-dd",
"order": {
"_term": "asc"
}
}
}
}
}
}
}
}
}
而且我的结果是这样的:
... omitted 4 documents that match filter criteria ...
"aggregations": {
"filtered_result": {
"doc_count": 4, <---- this is ok, I really have 4 docs that match criteria
"result": {
"doc_count": 12, <---- those 4 documents really have 12 events (together)
"NAME": {
"buckets": [
{
"key": 1414800000000,
"key_as_string": "2014-11-01",
"doc_count": 2
},
{
"key": 1414886400000,
"key_as_string": "2014-11-02",
"doc_count": 2
},
{
"key": 1414972800000,
"key_as_string": "2014-11-03",
"doc_count": 6
},
{
"key": 1415145600000,
"key_as_string": "2014-11-05",
"doc_count": 2
}
]
}
}
}
}
而且我希望得到的东西,如:
"buckets": [
{
"key_as_string": "2014-11-02",
"doc_count": 0
},
{
"key_as_string": "2014-11-03",
"doc_count": 2
},
{
"key_as_string": "2014-11-04",
"doc_count": 0
},
{
"key_as_string": "2014-11-05",
"doc_count": 2
}
]
基本上是匹配的标准4号文件是按日期分布是标准发生时,“2011-11-03”两篇文章,“2014-11-05”两篇文档(2014-11-02之后有4篇文档有事件“安装”,之后没有卸载事件安装)