2016-05-12 107 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