2016-05-12 108 views
1

如何對鍵上的elasticsearch聚合存儲桶進行排序。我嵌套聚合,並想排序我的第二個聚合桶結果。elasticsearch聚合排序存儲桶密鑰

像我有:

"result": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": 20309, 
       "doc_count": 752, 
       "Events": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
        { 
         "key": "impression", 
         "doc_count": 30 
        }, 
        { 
         "key": "page_view", 
         "doc_count": 10 
        }, 
        ... 
        ] 
       } 
      }, 
      { 
       "key": 20771, 
       "doc_count": 46, 
       "Events": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
        { 
         "key": "impression", 
         "doc_count": 32 
        }, 
        { 
         "key": "page_view", 
         "doc_count": 9 
        }, 
        ... 
        ] 
       } 
      }, 

我想我Events總水桶的遞減/遞增關鍵impressionpage_view排序。 我該如何實現這樣的結果集?

這裏是我的查詢

GET someindex/useractivity/_search?search_type=count 
{ 
    "size": 1000000, 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "range": { 
       "created_on": { 
        "from": "2015-01-12", 
        "to": "2016-05-12" 
       } 
       } 
      }, 
      { 
       "term": { 
       "group_id": 1 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "result": { 
     "terms": { 
     "field": "entity_id", 
     "size": 1000000 
     }, 
     "aggs": { 
     "Events": { 
      "terms": { 
      "field": "event_type", 
      "min_doc_count": 0, 
      "size": 10 
      } 
     } 
     } 
    } 
    } 
} 

我一直在使用_key嘗試,但它那種鬥內。我想通過查看所有桶來進行排序。就像我有一把鑰匙impression。我希望我的桶結果可以用這個鍵排序。不在鬥中。

我想我的結果設置爲一樣,如果我想降序排序上impression那麼我的結果應該是

"buckets": [ 
       { 
        "key": 20771, 
        "doc_count": 46, 
        "Events": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
         { 
          "key": "impression", 
          "doc_count": 32 
         }, 
         { 
          "key": "page_view", 
          "doc_count": 9 
         }, 
         ... 
         ] 
        } 
       }, 
       {      
        "key": 20309, 
        "doc_count": 752, 
        "Events": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
         { 
          "key": "impression", 
          "doc_count": 30 
         }, 
         { 
          "key": "page_view", 
          "doc_count": 10 
         }, 
         ... 
         ] 
        } 
       }, 

即以最大的印象桶應該在最前面。 (爲了通過水桶印象降序排列)

+0

請分享你正在運行的查詢。 – Richa

+1

通過使用''命令':{ 「_key」:「asc」 }'或者我錯過了什麼? –

+0

使用'_key'將在桶內排序。我想把它與所有水桶分類。就像我有一個關鍵的「印象」。我希望我的桶結果可以用這個鍵排序。不在桶內 – yakhtarali

回答

0

試試這個聚集:

{ 
    "size": 0, 
    "aggs": { 
    "result": { 
     "terms": { 
     "field": "entity_id", 
     "size": 10, 
     "order": { 
      "impression_Events": "desc" 
     } 
     }, 
     "aggs": { 
     "Events": { 
      "terms": { 
      "field": "event_type", 
      "min_doc_count": 0, 
      "size": 10 
      } 
     }, 
     "impression_Events": { 
      "filter": { 
      "term": { 
       "event_type": "impression" 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

我試過這個,但它不工作,因爲我需要。我注意到你來自@elastic,如果我沒有錯的話。那麼有什麼辦法可以實現這樣的結果集? – yakhtarali

+0

你能解釋一下你期望的不是什麼嗎? –

+0

我已更新我的問題,並顯示出期望的結果 – yakhtarali