【问题标题】:Elasticsearch - Limit docs count used for sum aggregationElasticsearch - 限制用于总和聚合的文档数
【发布时间】:2015-06-04 15:50:15
【问题描述】:

我知道它不应该那样工作,但是有没有办法强制求和聚合根据查询中设置的大小来限制总和?

就像在这个查询中一样:

{
    "size" : 10,
    "query":{
        "filtered":{
            "query":{
                "match_all":{}
            },
            "filter": {
                // some filter     
            }
        }
    },
    "aggs": {
        "value" : {
            "sum" :{
                "field":"value"
            }
         }
    }
}

如果我有 100 个文档,我想只检索 10 个文档以及这 10 个文档的总和。

简而言之:无论分数如何,我都需要一个 SELECT SUM(value) FROM table LIMIT 10。

你们知道我是否可以用 ES 做到这一点吗?

【问题讨论】:

    标签: elasticsearch sum limit


    【解决方案1】:

    limit filter 似乎可以满足您的需求。这是一个简单的例子。

    我建立了一个简单的索引并给了它一些文档:

    PUT /test_index
    {
       "settings": {
          "number_of_shards": 1
       }
    }
    
    POST /test_index/doc/_bulk
    {"index":{"_id":1}}
    {"name":"a a", "val": 1}
    {"index":{"_id":2}}
    {"name":"a b", "val": 2}
    {"index":{"_id":3}}
    {"name":"a c", "val": 3}
    {"index":{"_id":4}}
    {"name":"b a", "val": 4}
    {"index":{"_id":5}}
    {"name":"b b", "val": 5}
    {"index":{"_id":6}}
    {"name":"b c", "val": 6}
    

    然后我可以得到前两个文档的"val"字段与包含术语"name""a"的总和,如下:

    POST /test_index/_search
    {
       "query": {
          "filtered": {
             "query": {
                "term": {
                   "name": {
                      "value": "b"
                   }
                }
             },
             "filter": {
                "limit": {
                   "value": 2
                }
             }
          }
       },
       "aggs": {
          "val_sum": {
             "sum": {
                "field": "val"
             }
          }
       }
    }
    ...
    {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 0.73895097,
          "hits": [
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "2",
                "_score": 0.73895097,
                "_source": {
                   "name": "a b",
                   "val": 2
                }
             },
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "4",
                "_score": 0.73895097,
                "_source": {
                   "name": "b a",
                   "val": 4
                }
             }
          ]
       },
       "aggregations": {
          "val_sum": {
             "value": 6,
             "value_as_string": "6.0"
          }
       }
    }
    

    这是我使用的代码:

    http://sense.qbox.io/gist/6be3fc75db339fa3810521dbcb61429cd885d1bf

    【讨论】:

    • 谢谢你,斯隆。限制过滤器确实是一个很好的方法,但是随着 per shard 应用限制,它仍然不会按照我想要的方式限制文档。真可惜。你知道是否有办法限制每个索引返回的文档?
    • 哦,对了,我错过了。抱歉,我暂时想不出任何东西。
    • 谢谢,斯隆。要知道,我能想出的唯一解决方案是设置单个分片或(可怕,我知道)“limit”/“shards_number”。不过我两个都不会用。
    猜你喜欢
    • 2022-09-24
    • 2019-06-14
    • 2021-11-14
    • 2017-04-22
    • 2016-09-24
    • 2014-05-02
    • 2016-04-14
    • 2015-06-07
    • 1970-01-01
    相关资源
    最近更新 更多