【问题标题】:Elasticsearch counts of multiple indices多个索引的 Elasticsearch 计数
【发布时间】:2015-02-12 16:27:42
【问题描述】:

我正在创建一个报告来比较数据库中的实际计数与索引记录。

我有三个索引 index1、index2 和 index3

要获取单个索引的计数,我使用以下 URL

http://localhost:9200/index1/_count?q=_type:invoice

=> {"count":50,"_shards":{"total":5,"successful":5,"failed":0}}

对于多个索引:

http://localhost:9200/index1,index2/_count?q=_type:invoice

=> {"count":80,"_shards":{"total":5,"successful":5,"failed":0}}

现在计数已加起来,我希望它按索引分组,我如何按特定字段传递过滤器组 得到这样的输出:

{"index1_count":50,"index2_count":50,"approved":10,"rejected":40 ,"_shards":{"total":5,"successful":5,"failed":0}}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以使用_search?search_type=count 并根据_index 字段进行聚合以区分索引:

    GET /index1,index2/_search?search_type=count
    {
      "aggs": {
        "by_index": {
          "terms": {
            "field": "_index"
          }
        }
      }
    }
    

    结果会是这样的:

    "aggregations": {
          "by_index": {
             "doc_count_error_upper_bound": 0,
             "sum_other_doc_count": 0,
             "buckets": [
                {
                   "key": "index1",
                   "doc_count": 50
                },
                {
                   "key": "index2",
                   "doc_count": 80
                }
             ]
          }
       }
    

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多