【发布时间】:2018-05-21 05:42:14
【问题描述】:
情况:
我是 Elasticsearch 的初学者,无法思考如何使用聚合来获得我需要的东西。
我有以下结构的文档:
{
...
"authors" : [
{
"name" : "Bob",
"@type" : "Person"
}
],
"resort": "Politics",
...
}
我想使用聚合来获取每个作者的文档数。由于某些文档可能有多个作者,因此这些文档应单独计算每个作者。
我的尝试:
由于terms 聚合与resort 字段一起使用,我尝试将它与authors 或name 字段一起使用,但始终没有得到任何桶。为此,我使用了以下curl 请求:
curl -X POST 'localhost:9200/news/_doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"_source": false,
"aggs": {
"author_agg": { "terms": {"field": "authors.keyword" } }
}
}'
我得出结论,terms 聚合不适用于列表中包含的字段。
接下来我想到了nested聚合,但是文档说,它是一个
单桶聚合
所以不是我要寻找的。因为我的想法用完了,所以我尝试了它,但得到了错误
"type" : "aggregation_execution_exception",
"reason" : "[nested] nested path [authors] is not nested"
我找到了this answer 并尝试将其用于我的数据。我有以下要求:
curl -X GET "localhost:9200/news/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"nest": {
"nested": {
"path": "authors"
},
"aggs": {
"authorname": {
"terms" : {
"field": "name.keyword"
}
}
}
}
}
}'
这给了我错误
"type" : "aggregation_execution_exception",
"reason" : "[nested] nested path [authors] is not nested"
我搜索了如何使用映射使我的路径嵌套,但我不知道如何实现。我什至不知道这是否真的有意义。
那么,如何根据位于文档内列表元素中的键将文档聚合到存储桶中?
也许这个问题已经在其他地方得到了回答,但是我无法以正确的方式陈述我的问题,因为我仍然对所有新信息感到困惑。提前感谢您的帮助。
【问题讨论】:
标签: elasticsearch elasticsearch-aggregation