elasticsearch 包括扁平字段的键和值的聚合

0ejtzxu1  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(106)

比方说,我有这份文件:

POST bug_reports/_doc/1
  {
    "title": "Results are not sorted correctly.",
    "labels": {
      "priority": "urgent",
      "product": "A"
    }
  }

labels字段是平坦的,这意味着我们不知道键的数量/名称。
如果我得到一个聚合,它会为bucket提供如下所示的内容:

{
  "buckets": [
    {
      "key": "urgent",
      "doc_count": 27705
    },
    {
      "key": "no urgent",
      "doc_count": 705
    }
  ]
}

这是预期的结果,但我想创建一个聚合,包括键,值和每个键在扁平字段内的doc_count。

{
  "buckets": [
    {
      "key": "priority",
      "values": [
        {
          "key": "urgent",
          "doc_count": 27705
        },
        {
          "key": "no urgent",
          "doc_count": 705
        }
      ]
    },
    {
      "key": "product",
      "values": [
        {
          "key": "A",
          "doc_count": 3215
        }
      ]
    }
  ]
}

这是可能的吗?我已经检查了与compositemulti_terms聚合相关的文档,但它们似乎是针对不同的用例。谢谢!

jdg4fx2g

jdg4fx2g1#

可以在一个查询中使用多个聚合。例如:

GET bug_reports/_search
{
  "size": 0,
  "aggs": {
    "priority_key": {
      "terms": {
        "field": "labels.priority",
        "size": 10
      }
    },
    "product_key": {
      "terms": {
        "field": "labels.product",
        "size": 10
      }
    }
  }
}

聚合all fields inside an object automatically是不可能的,但是可以在聚合之前找到并定义它。

GET bug_reports/_mapping/field/labels.*?filter_path=*.*.*.full_name

答复:

{
  "bug_reports": {
    "mappings": {
      "labels.priority": {
        "full_name": "labels.priority"
      },
      "labels.product": {
        "full_name": "labels.product"
      }
    }
  }
}

相关问题