【发布时间】:2019-08-31 12:47:05
【问题描述】:
如标题所示,我在按文本字段对 Elasticsearch 聚合进行排序时遇到问题。有没有可能做到这一点?使用热门歌曲或类似的东西?现在我正在使用术语聚合,我可以使用 _term 按聚合字段排序,但我需要按不同字段对这些聚合进行排序。我知道如何处理具有数值的字段。例如使用 max、min、sum 等。
如果我能这样(但我做不到)那就太好了:
"aggs": {
"Variants": {
"terms": {
"field": "variant",
"order": {
"top_Song_hits": "asc"
}
},
"aggs": {
"top_Song_hits": {
"sum": {
"name": {
"order": "desc"
}
}
}
}
}
}
}
或者像这样:
{
"aggs": {
"Variants": {
"terms": {
"field": "variant",
"order": {
"name_agg": "asc"
}
},
"aggs": {
"name_agg": {
"terms": {
"field": "name"
}
}
}
}
}
}
或者
{
"aggs": {
"Variants": {
"terms": {
"field": "variant",
"order": {
"details": "asc"
}
},
"aggs": {
"details": {
"top_hits": {
"size": 1,
"_source": {
"include": ["name"]
}
}
}
}
}
}
}
在最后一种情况下我得到错误:
"reason": "Invalid aggregation order path [details]. Buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
【问题讨论】:
-
为什么你的例子不起作用?它似乎与the examples in the docs 匹配。 “我不能”根本没有帮助。另外:
_term已弃用;现在使用_key。 -
"...现在我正在使用术语聚合,我可以使用 _term 按聚合字段排序,但我需要按不同字段对这些聚合进行排序..." 我不想按 _key 排序.例如我想按名称排序
-
name 是文档字段吗?那没有意义。 terms 是一个桶聚合。每个存储桶包含许多文档,因此没有单一的名称可供排序。
-
好的,我知道这没有意义,但我需要实现这样的目标。也许我需要使用不同的术语,但我不知道是什么。
-
在我的情况下,我有产品列表,我需要按 variant_id 对它们进行分组并按产品名称对其进行排序。
标签: php sorting elasticsearch