要不要也来分享分享一下啊

terms聚合排序

Elasticsearch | 作者 alivinliu | 发布于2018年11月28日 | 阅读数:5632

es可不可以实现利用子terms聚合的某个term的_count进行排序?

例如: 
姓名 性别
tom 男
jack 女
jack 女
 
期望得到如下结果: (按照子聚合term(男)的_count来排序)
{
"key": "tom",
"doc_count": 1,
"s1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "男",
"doc_count": 1
}
]
}
}
,
{
"key": "jack",
"doc_count": 2,
"s1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "女",
"doc_count": 2
}
]
}
}
已邀请:

zz_hello

赞同来自: alivinliu

花了一个多小时,终于给你弄了一个方案。。。你看看吧
GET bank/_search
{
"size": 0,
"aggs": {
"first": {
"terms": {
"field": "name.keyword",
"size": 10,
"order": {
"gl": "desc"
}
},
"aggs": {
"gl": {
"filter": {
"term": {
"gender.keyword": "M"
}
}
},
"second": {
"terms": {
"field": "gender.keyword"
}
}
}
}
}
}
因为性别不是数值字段,所以不能用指标聚合,这里采用过滤桶,只选取男的文档。然后用这个的doc_count进行排序。最后在second聚合中显示了所有的性别。

rochy - rochy_he

赞同来自:

可以按照子聚合的内容进行排序,具体参考:https://www.elastic.co/guide/e ... order
 
下面是样例,自己参考着改一下
GET /_search
{
"aggs" : {
"genres" : {
"terms" : {
"field" : "genre",
"order" : { "playback_stats.max" : "desc" }
},
"aggs" : {
"playback_stats" : { "stats" : { "field" : "play_count" } }
}
}
}
}
 
 
 

要回复问题请先登录注册