首页 > 解决方案 > 如何按热门字段(文本字段)对聚合进行排序?或者是否有可能按文本字段对聚合进行排序(不使用_term)

问题描述

如标题所示,我在按文本字段对 Elasticsearch 聚合进行排序时遇到问题。有没有可能做到这一点?使用热门歌曲或类似的东西?现在我正在使用术语聚合,我可以使用 _term 按聚合字段排序,但我需要按不同字段对这些聚合进行排序。我知道如何处理具有数值的字段。例如使用最大值、最小值、总和等。

如果我能这样做(但我不能),那就太好了:

    "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."

标签: phpsortingelasticsearch

解决方案



推荐阅读