三人行必有我师

composite 多字段聚合按照count值降序返回

Elasticsearch | 作者 elasticStack | 发布于2019年11月06日 | 阅读数:439

1.es6.8版本
2.目前使用composite 方式实现多字段聚合,遇到的问题就是想按照count值降序返回结果, 但是官方文档没有说明该方式, 只列举了按照key的降序和升序返回的方式, 请问有遇到类似情况的吗, 指教一下
已邀请:

God_lockin

赞同来自: elasticStack

参考下:社区回答
 
说是不可以
 


To answer your first question, no, there isn't a way to order by doc count with the composite aggregation. Ordering would require passing over the entire dataset first and keeping a record of how many docs each term has, which would require memory equivalent to the number of terms.

That's opposite of what the composite agg is made for: it's designed as a memory-friendly way to paginate over aggregations. Part of the tradeoff is that you lose things like ordering by doc count, since that isn't known until after all the docs have been collected.


 
 

God_lockin

赞同来自:

这个不是keyword聚合的count的倒序么?贴个你的数据出来试试?
 
PUT test_index
{
"settings": {
"refresh_interval": "1s",
"number_of_shards": "1",
"number_of_replicas": "0"
},
"mappings": {
"properties": {
"keyword": {
"type": "keyword"
},
"number": {
"type": "integer"
}
}
}
}
 
POST test_index/_doc
{
"keyword": ["foo", "bar"],
"number": [23, 65, 76]
}
 
{
"size": 0,
"aggs": {
"my_buckets": {
"composite": {
"sources": [
{
"product": {
"terms": {
"field": "keyword",
"order": "desc"
}
}
}
]
}
}
}
}

elasticStack - 90后it大数据男

赞同来自:

你这个不是官网的例子吗, 而且order这个只是针对聚合字段起作用, 没有体现count值呀

God_lockin

赞同来自:

并不会啊,你看里面的doc_count⬇️
DSL1
POST test_index/_search
{
"size": 0,
"aggs": {
"my_buckets": {
"composite": {
"sources": [
{
"product": {
"terms": {
"field": "keyword",
"order": "desc"
}
}
}
]
}
}
}
}
Result1:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 15,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"my_buckets" : {
"after_key" : {
"product" : "bar"
},
"buckets" : [
{
"key" : {
"product" : "foo"
},
"doc_count" : 11
},
{
"key" : {
"product" : "bar"
},
"doc_count" : 8
}
]
}
}
}
DSL2
POST test_index/_search
{
"size": 0,
"aggs": {
"my_buckets": {
"composite": {
"sources": [
{
"product": {
"terms": {
"field": "keyword"
}
}
}
]
}
}
}
}
re sult2
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 15,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"my_buckets" : {
"after_key" : {
"product" : "foo"
},
"buckets" : [
{
"key" : {
"product" : "bar"
},
"doc_count" : 8
},
{
"key" : {
"product" : "foo"
},
"doc_count" : 11
}
]
}
}
}

laoyang360 - Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

官方7.5 推出了稀有聚合 rare terms aggregation 就是解决您的问题的。建议看一下。

要回复问题请先登录注册