首页 > 解决方案 > 按 _source 中的字段聚合

问题描述

我在 elasticsearch 中有一个索引,其中包含如下所示的文档:

 "hits": [
        {
            "_index": "my-index2",
            "_type": "my-type",
            "_id": "1",
            "_score": 1,
            "_source": {
                "entities": {
                    "persons": [
                        "Kobe Bryant",
                        "Michael Jordan"
                    ],
                    "dates": [
                        "Yesterday"
                    ],
                    "locations": [
                        "Munich",
                        "New York"
                    ]
                },
                "my_field": "Kobe Bryant was one of the best basketball players of all times. Not even Michael Jordan has ever scored 81 points in one game. Munich is really an awesome city, but New York is as well. Yesterday has been the hottest day of the year."
            }
        }

是否可以使用聚合函数按entities对象中的字段进行聚合?我试过了,但没有用

{
    "aggs" : {
        "avg_date" : {
            "avg" : {
                "script" : {
                    "source" : "doc.entities.dates"
                }
            }
        }
    }
}

错误说我的索引没有entities字段。

编辑:使用以下术语聚合查询:

{
    "aggs" : {
        "dates" : {
            "terms" : { "field" : "entities.dates" }
        }
    }
}

我收到一条错误消息

默认情况下,在文本字段上禁用 Fielddata。在 [entities.dates] 上设置 fielddata=true,以便通过反转倒排索引将 fielddata 加载到内存中。

我可以像错误所说的那样设置 fielddata=true 但是文档警告不要这样做,因为它使用了大量的堆空间。还有其他方法可以执行此查询吗?

编辑 2:通过将所有字段设置entities为索引中的关键字来解决此问题。

标签: elasticsearchelasticsearch-aggregation

解决方案


推荐阅读