如何在ElasticSearch结果中隐藏存储桶?

时间:2016-09-01 15:08:49

标签: elasticsearch

在我的查询中,我在一个标量中聚合了桶。由于我对每个桶(在我的情况下,数千万)不感兴趣,我想从返回的结果中删除它们;即我想做一些像“大小”的东西:0来隐藏所有的命中。有可能吗?

E.g:

{
  "size": 0,
  "aggs": {
    "pop": {
      "terms": {
        "field": "account_number",
        "size": 0
      },
      "aggs": { 
            "average": { 
               "avg": {
                  "field": "price" 
               }
            }
         }
    },
    "sum_of_avg": {
      "sum_bucket": {
        "buckets_path": "pop>average"
      }
    }
  }
}

结果:

[...]
"aggregations": {
    "pop": {
      "doc_count_error_upper_bound": 40851,
      "sum_other_doc_count": 93441329,
      "buckets": [...] <== i don't want this
    },
    "sum_of_avg": {
      "value": 128.0768325884469
    }

2 个答案:

答案 0 :(得分:1)

我刚刚在相关问题中发布了answer

在这种情况下,请求应如下所示:

curl -XPOST 'http://localhost:9200/<index>/_search?filter_path=aggregations.sum_of_avg' -d '
{
  "size": 0,
  "aggs": {
    "pop": {
      "terms": {
        "field": "account_number",
        "size": 0
      },
      "aggs": { 
            "average": { 
               "avg": {
                  "field": "price" 
               }
            }
         }
    },
    "sum_of_avg": {
      "sum_bucket": {
        "buckets_path": "pop>average"
      }
    }
  }
}

PS:如果您找到了其他解决方案,请在此处分享。谢谢!

答案 1 :(得分:-1)

我认为你想要的是“基数”聚合。这将返回不同值的数量。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html

示例:

GET devdev/alert/_search
{
  "size": 0,
  "aggs": {
    "agg1": {
      "cardinality": {
        "field": "price"
      }
    }
  }
}