2015-08-21 147 views
0

我有以下的ElasticSearch文檔列表(其中分數nested):在ElasticSearch中過濾嵌套聚合?

{ 
    'type': 'typeA', 
    'scores': [ 
     {'type': 'A', 'val': 45}, 
     {'type': 'A', 'val': 55}, 
     {'type': 'B', 'val': 65}, 
    ] 
}, 
{ 
    'type': 'typeA', 
    'scores': [ 
     {'type': 'A', 'val': 55}, 
     {'type': 'A', 'val': 50}, 
     {'type': 'A', 'val': 57}, 
    ] 
}, 
{ 
    'type': 'typeB', 
    'scores': [ 
     {'type': 'B', 'val': 40}, 
     {'type': 'A', 'val': 50}, 
     {'type': 'A', 'val': 60}, 
    ] 
} 

是否有可能有一個每type返回平均成績查詢,但只有當scores.type是「A」?

解釋(如果我手動做到了):

1)只過濾器 「A」 得分(簡化):

{'type': 'typeA', 'scores': [45, 55]}, 
{'type': 'typeA', 'scores': [55, 50, 57]}, 
{'type': 'typeB', 'scores': [50, 60]}, 

2)找到每個文檔AVG:

{'type': 'typeA', 'avg': 50}, // (45+55)/2 
{'type': 'typeA', 'avg': 54}, // (55+50+57)/3 
{'type': 'typeB', 'avg': 55}, // (50 + 60)/2 

3)每種類型的最終聚合:

'typeA' : 52, // (50+54)/2 
'typeB': 55, // (55)/1 

是否有可能或我應該堅持客戶端爲此?

回答

1

是的,這是絕對有可能與termsnestedavg聚合的組合來做到這一點,就像這樣:

{ 
    "size": 0, 
    "aggs": { 
    "top_level_type": {     <---- group by top-level type 
     "terms": { 
     "field": "type" 
     }, 
     "aggs": { 
     "nest": { 
      "nested": {      <---- "dive" your nested scores 
      "path": "scores" 
      }, 
      "aggs": { 
      "type_filter": { 
       "filter": {     <---- filter only score type A 
       "term": { 
        "scores.type": "A" 
       } 
       }, 
       "aggs": { 
       "average": { 
        "avg": {     <---- compute the average of the score values 
        "field": "scores.val" 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

所得的值應該是這樣的:

{ 
    ... 
    "aggregations" : { 
    "top_level_type" : { 
     "doc_count_error_upper_bound" : 0, 
     "sum_other_doc_count" : 0, 
     "buckets" : [ { 
     "key" : "typea", 
     "doc_count" : 2, 
     "nest" : { 
      "doc_count" : 6, 
      "type_filter" : { 
      "doc_count" : 5, 
      "average" : { 
       "value" : 52.4 
      } 
      } 
     } 
     }, { 
     "key" : "typeb", 
     "doc_count" : 1, 
     "nest" : { 
      "doc_count" : 3, 
      "type_filter" : { 
      "doc_count" : 2, 
      "average" : { 
       "value" : 55.0 
      } 
      } 
     } 
     } ] 
    } 
    } 
} 
+0

有一個計算錯誤:計算所有文檔的平均值:(45 + 55 + 55 + 50 + 57)/ 5 = 52.4,但它應該計算單個文檔中每個分數的平均值,然後計算每個文檔的平均值:((45+ 55)/ 2 +(55 + 50 + 57)/ 3)/ 2 = 52.0 – dmzkrsk

+0

好的,我明白你的意思了。問題在於,對於聚合,沒有「文檔」的概念。但是,您可以做的是在索引時計算每種類型的平均值,並將其存儲爲文檔中的頂級字段(在第一個文檔中,您將具有'typea_avg:50'和'typeb_avg:60'等)。那麼你只需要在這些字段上運行「平均」聚合。 – Val