2014-11-04 226 views
9

我在過濾聚合結果後遇到問題。我認爲我走在正確的軌道上,但我覺得我正在追逐我的尾巴。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篇文檔有事件「安裝」,之後沒有卸載事件安裝)

回答

1

這是部分答案。

還有一個主要問題:根據你的數據,實際上是沒有文件,將符合您的要求,所以我加了一些:

curl -XPUT 'localhost:9200/my_index/reporting/9' -d '{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    } 
] 
}' 

curl -XPUT 'localhost:9200/my_index/reporting/10' -d '{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-01" 
    } 
    ] 
}' 

爲了能夠運用邏輯,我改變了架構中,以便該事件也包含在父項中 - 這樣您可以搜索「沒有任何UNINSTALL事件」。因爲事情是,在嵌套搜索中,您總是隻查看一個單一事件,因此您無法進行任何類型的「報告範圍」搜索。

curl -XPUT 'localhost:9200/my_index' -d '{ 
    "mappings": { 
    "reporting": { 
     "properties": { 
     "events": { 
      "type": "nested", "include_in_root": true, 
      "properties": { 
      "name": { "type": "string", "index" : "not_analyzed" }, 
      "date": { "type": "date" } 
      } 
     } 
     } 
    } 
    } 
}' 

而現在是查詢本身。看起來,當使用嵌套過濾器時,不能直接進入「過濾器」。你必須先做「查詢>過濾>過濾器」的東西。

一般來說,寫一個長的elasticsearch查詢的提示 - 記住除了「must」和「must_not」之外,還有「and」和「or」運算符 - 就是將它寫出代碼。你的情況:

has_one(event.name == 'INSTALL' && event.date >= '2014-11-02') 
&& has_none(event.name == 'UNINSTALL') 
&& has_none(event.name == 'UNINSTALL' && event.date >= '2014-11-02') 

或者:

has_one(event.name == 'INSTALL' && event.date >= '2014-11-02') 
&& (has_none(event.name == 'UNINSTALL') 
    || has_only(event.name == 'UNINSTALL' && event.date >= '2014-11-02')) 

我能適用所有,但最後has_only/has_none。爲此,您可能想嘗試使用子文檔。在那裏,你至少可以在must_not bool下使用has_child過濾器。

當前查詢:

GET /my_index/reporting/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "and": { 
      "filters": [ 
      { 
       "or": { 
       "filters": [ 
        { 
        "bool": { 
         "must_not": [ 
         { 
          "term": { 
          "events.name": "UNINSTALL" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "nested": { 
         "path": "events", 
         "query": { 
         "filtered": { 
          "filter": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
            "name": "UNINSTALL" 
            } 
           }, 
           { 
            "range": { 
            "date": { 
             "lt": "2014-11-02" 
            } 
            } 
           } 
           ] 
          } 
          } 
         } 
         } 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "query": { 
        "filtered": { 
        "filter": { 
         "bool": { 
         "must": [ 
          { 
          "term": { 
           "name": "INSTALL" 
          } 
          }, 
          { 
          "range": { 
           "date": { 
           "gte": "2014-11-02" 
           } 
          } 
          } 
         ] 
         } 
        } 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggregations": { 
    "filtered_result": { 
     "filter": { 
     "and": { 
      "filters": [ 
      { 
       "or": { 
       "filters": [ 
        { 
        "bool": { 
         "must_not": [ 
         { 
          "term": { 
          "events.name": "UNINSTALL" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "nested": { 
         "path": "events", 
         "query": { 
         "filtered": { 
          "filter": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
            "name": "UNINSTALL" 
            } 
           }, 
           { 
            "range": { 
            "date": { 
             "lt": "2014-11-02" 
            } 
            } 
           } 
           ] 
          } 
          } 
         } 
         } 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "query": { 
        "filtered": { 
        "filter": { 
         "bool": { 
         "must": [ 
          { 
          "term": { 
           "name": "INSTALL" 
          } 
          }, 
          { 
          "range": { 
           "date": { 
           "gte": "2014-11-02" 
           } 
          } 
          } 
         ] 
         } 
        } 
        } 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggs": { 
     "result": { 
      "nested": { 
      "path": "events" 
      }, 
      "aggs": { 
      "NAME": { 
       "terms": { 
       "field": "date", 
       "format": "yyyy-MM-dd", 
       "order": { 
        "_term": "asc" 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}