【问题标题】:Document Count in keyword buckets from list in document as aggregation in Elasticsearch文档列表中关键字存储桶中的文档计数作为 Elasticsearch 中的聚合
【发布时间】:2018-05-21 05:42:14
【问题描述】:

情况:

我是 Elasticsearch 的初学者,无法思考如何使用聚合来获得我需要的东西。

我有以下结构的文档:

{
    ...
    "authors" : [
      {
        "name" : "Bob",
        "@type" : "Person"
      }
    ],
    "resort": "Politics",
    ...
}

我想使用聚合来获取每个作者的文档数。由于某些文档可能有多个作者,因此这些文档应单独计算每个作者。

我的尝试:

由于terms 聚合与resort 字段一起使用,我尝试将它与authorsname 字段一起使用,但始终没有得到任何桶。为此,我使用了以下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


    【解决方案1】:

    我终于解决了我的问题:

    获得authors 键映射nested 的想法是完全正确的。但不幸的是,Elasticsearch 不允许您直接将类型从 un-nested 更改为 nested,因为该键中的所有项目也必须被索引。所以你必须走以下路:

    1. 使用自定义映射创建新索引。在这里,我们进入文档类型_doc,进入它的属性,然后进入文档字段authors。我们将type 设置为nested

    ~

    curl -X PUT "localhost:9200/new_index?pretty" -H 'Content-Type: application/json' -d'
    {
      "mappings": {
        "_doc" : {
          "properties" : {
            "authors": { "type": "nested" }
          }
        }
      }
    }'
    
    1. 然后我们重新索引我们的数据集并将目标设置为我们新创建的索引。这会将旧索引中的数据索引到新索引中,本质上是复制纯数据,但采用新映射(因为不会以这种方式复制设置和映射)。

    ~

    curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "old_index"
      },
      "dest": {
        "index": "new_index"
      }
    }'
    

    现在我们可以在这里进行nested 聚合,根据作者将文档分类到桶中:

    curl -X GET 'localhost:9200/new_index/_doc/_search?pretty' -H 'Content-Type: application/json' -d'
    {
      "size": 0,
      "aggs": {
        "authors": {
          "nested": {
            "path": "authors"
          },
          "aggs": {
            "authors_by_name": {
              "terms": { "field": "authors.name.keyword" }
            }
          }
        }
      }
    }'
    

    直到现在我才知道如何重命名索引,但您当然可以简单地删除旧索引,然后按照描述的过程创建另一个新索引,使用旧索引的名称但自定义映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多