Elasticsearch - Limit docs count used for sum aggregation -
i know it's not supposed work way, there way force sum aggregations limit sum based on size set in query?
like in query:
{ "size" : 10, "query":{ "filtered":{ "query":{ "match_all":{} }, "filter": { // filter } } }, "aggs": { "value" : { "sum" :{ "field":"value" } } } }
if have 100 docs, i'd retrieve 10 docs , sum of these 10 docs.
in nutshell: need select sum(value) table limit 10, regardless score.
do guys know if can es?
the limit filter seems want. here simple example.
i set simple index , gave docs:
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}
then can sum of "val"
field of first 2 documents "name"
containing term "a"
, follows:
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" } } }
here code used:
http://sense.qbox.io/gist/6be3fc75db339fa3810521dbcb61429cd885d1bf
Comments
Post a Comment