繁体   English   中英

如何根据单个字段中的值和每个桶的计数获得多个字母聚合

[英]How can I get multiple alphabetic aggregates base on values in a single field and each buckets' count

我有一个字段:name.keyword。 我希望聚合(或其中的一组按层次结构细分)结果如下所示:

all_names - a count of all distinct values for the name.keyword field
     names_a_through_e - a count of all distinct values that are limited to those values that start with "A" through those that start with "E"
          names_a - a count of all distinct values that are limited to those that start with "A"
               names - a list of each of these names and their count
          names_b - a count of all distinct values that are limited to those that start with "B"
               names - a list of each of these names and their count
          ...
     names_f_through_j - a count of all distinct values that are limited to those values that start with "F" through those that start with "J"
          names_f - a count of all distinct values that are limited to those that start with "F"
               names - a list of each of these names and their count
          names_g - a count of all distinct values that are limited to those that start with "G"
               names - a list of each of these names and their count
          ...
     ...

我当然可以涵盖最内部的列表,如下所示:

    "a_names": {
        "terms": {
            "field": "name.keyword",
            "include": "A.*",
            "size": 100,
            "order": {"_term": "asc"}
        }
    }

但这并没有给我一个级别的计数 - name.keyword 字段中带有 A* 的所有文档的总和。

或者有没有办法在单个字段上执行这种结构化分桶,这样可以像我描述的那样很好地组织布局?

您可以使用sum_bucket 聚合来计算存储桶中的文档总数。

询问

 {
  "size": 0,
  "aggs": {
    "a_names": {
      "terms": {
        "field": "name.keyword",
        "include": "A.*",
        "size": 100,
        "order": {
          "_term": "asc"
        }
      }
    },
    "totalcount": {
      "sum_bucket": {
        "buckets_path": "a_names._count"
      }
    }
  }
}

结果:

"hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "a_names" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Abc",
          "doc_count" : 1,
          "Count" : {
            "value" : 1
          }
        },
        {
          "key" : "Acf",
          "doc_count" : 1,
          "Count" : {
            "value" : 1
          }
        }
      ]
    },
    "totalcount" : {
      "value" : 2.0
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM