2015-06-15 89 views
0

我正在處理Elasticsearch聚合,我想要獲得一個聚合所有記錄值的單個值。我可以使用「Range> = 0」聚合來作弊,但是有沒有更習慣的方式?從Elasticsearch返回一個合計值

我的缺憾查詢:

{ 
    "size": 0, 
    "aggs": { 
    "all": { 
     "range": { 
     "field": "price", 
     "ranges": [{"from": 0}] } 
     } 
    }, 
    "aggs": { 
     "total price": { 
     "avg": {"field": "price"}} 
     } 
    } 
} 

回答

1

ES已經內置在「統計」聚集這對你來說,使用「求和」的結果。

GET devdev/alert/_search 
{ 
    "size": 0, 
"aggs": { 
    "ag1": { 
    "stats": { 
     "field": "price" 
    } 
    } 
} 
} 

結果:

{ 
    "took": 483, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 4129196, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "ag1": { 
     "count": 4119334, 
     "min": -1, 
     "max": 7004, 
     "avg": 5.29581966405249, 
     "sum": 21815250 
     } 
    } 
}