计算ElasticSearch中每个嵌套字段的元素数

ssm49v7z  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(153)

我是Elastic Search的新手。我在Elastic Search中的一些文档包含如下嵌套字段:
文件一:

"Volume": [
{
"partition": "s1",
"type": "west"
}
{
"partition": "s2",
"type": "south"
}
]

文件二:

"Volume": [
{
"partition": "a2",
"type": "north"
}
]

文件三:

"Volume": [
{
"partition": "f3",
"type": "north"
}
{
"partition": "a1",
"type": "south"
}
]

等等。我需要计算“type”字段的数量,因此预期结果将是:“西”:1“南”:2“北”:2
我使用了嵌套聚合,如下所示:

"size":0,
  "aggs": {
    "nested_properties": {
      "nested": {
        "path": "Volume"
      },
      "aggs": {
        "count": {
          "cardinality": {
            "field": "Volume.type"
              }
          }
      }
   }
}

但结果是:

"aggregations": {
  "nested_properies": {
    "doc_count": 123456,
      "count": {
        "value": 9
      }
   }
}

如何计算每个“类型”子字段的条目数?

jv4diomz

jv4diomz1#

您可以使用“词汇汇总”。
就像这样:

{
  "size": 0,
  "aggs": {
    "groups": {
      "nested": {
        "path": "Volume"
      },
      "aggs": {
        "NAME": {
          "terms": {
            "field": "Volume.type.keyword",
            "size": 10
          }
        }
      }
    }
  }
}

回应:

"aggregations": {
    "groups": {
      "doc_count": 5,
      "NAME": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "north",
            "doc_count": 2
          },
          {
            "key": "south",
            "doc_count": 2
          },
          {
            "key": "west",
            "doc_count": 1
          }
        ]
      }
    }
  }

相关问题