【问题标题】:Return just buckets size of aggregation query - Elasticsearch只返回聚合查询的桶大小 - Elasticsearch
【发布时间】:2019-04-16 00:12:29
【问题描述】:

我在 elasticsearch 2.1 上使用聚合查询,这是我的查询:

"aggs": { 
    "atendimentos": {
      "terms": {
        "field": "_parent",
        "size" : 0
      }
    }
  }

返回是这样的:

"aggregations": {
        "atendimentos": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "1a92d5c0-d542-4f69-aeb0-42a467f6a703",
                    "doc_count": 12
                },
                {
                    "key": "4e30bf6d-730d-4217-a6ef-e7b2450a012f",
                    "doc_count": 12
                }.......

它返回 40000 个桶,所以我在这个聚合中有很多桶,我只想返回桶的大小,但我想要这样的东西:

buckets_size: 40000

各位,如何只返回桶大小?

好的,谢谢大家。

【问题讨论】:

    标签: elasticsearch nosql elasticsearch-aggregation


    【解决方案1】:

    试试这个查询:

    POST index/_search
    {
      "size": 0,
      "aggs": {
        "atendimentos": {
          "terms": {
            "field": "_parent"
          }
        },
        "count":{
          "cardinality": {
            "field": "_parent"
          }
        }
      }
    }
    

    它可能会返回类似的东西:

    "aggregations": {
        "aads": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "aa",
              "doc_count": 1
            },
           {
              "key": "bb",
              "doc_count": 1
            }
          ]
        },
        "count": {
          "value": 2
        }
      }
    

    编辑:更多信息在这里 - https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-aggregations-metrics-cardinality-aggregation.html

    【讨论】:

      【解决方案2】:
      {
          "aggs" : {
              "type_count" : {
                  "cardinality" : {
                      "field" : "type"
                  }
              }
          }
      }
      

      阅读更多关于Cardinality Aggregation的信息

      【讨论】:

        猜你喜欢
        • 2018-10-01
        • 1970-01-01
        • 2018-12-12
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        相关资源
        最近更新 更多