首页 > 解决方案 > 术语聚合返回多个字段 (min_doc_count: 0)

问题描述

我正在进行术语聚合,但我想返回多个字段。我希望用户通过“slug”(我的名字)选择存储桶,但显示实际的“名字”(我的名字)。

此刻,我正在制作这样的TopHits SubAggregation:

"organisation": {
    "aggregations": {
        "label": {
            "top_hits": {
                "_source": {
                    "includes": [
                        "organisations.name"
                    ]
                },
                "size": 1
            }
        }
    },
    "terms": {
        "field": "organisations.slug",
        "min_doc_count": 0,
        "size": 20
    }
}

当我的整个查询实际上找到一些存储桶/结果时,这会给出所需的结果。

你看我已经设置了min_doc_countto 0which 将返回文档计数为 0 的存储桶。我在这里面临的问题是我的 TopHits 响应是空的,这导致无法向客户端呈现正确的名称。

示例响应:

"organisation": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
        "key": "my-name",
        "doc_count": 27,
        "label": {
            "hits": {
                "total": 27,
                "max_score": 1,
                "hits": [
                {
                    "_index": "users",
                    "_type": "doc",
                    "_id": "4475",
                    "_score": 1,
                    "_source": {
                    "organisations": [
                        {
                            "name": "My name"
                        }]
                    }
                }]
            }
        }
    },
    {
        "key": "my-name-2",
        "doc_count": 0,
        "label": {
            "hits": {
                "total": 0,
                "max_score": null,
                "hits": []
            }
        }
    },
    .....

有人实现了这个预期的结果吗?我觉得 TopHits 在这里帮不了我。它应该始终获取名称。

我也尝试过:

我认为可能是一个解决方案,但感觉很脏:

亲切的问候,在此先感谢

标签: elasticsearchelasticsearch-aggregation

解决方案


推荐阅读