弹性搜索中的求和查询 [英] Sum Query in Elasticsearch
本文介绍了弹性搜索中的求和查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT lane,SUM(routes)FROM lane通过GROUP BY lane
我有这样的数据在ES中看起来像这样:
{
_index:kpi
_type:mroutes_by_lane,
_id:TUeWFEhnS9q1Ukb2QdZABg,
_score:1.0,
_source:{
warehouse_id:107,
date:2013-04-08,
lane:M05,
routes:4047
}
{
_index:kpi,
_type:mroutes_by_lane,
_id:owVmGW9GT562_2Alfru2DA,
_score :$,
_source:{
warehouse_id:107,
date:2013-04-08,
lane:M03 ,
路线:4065
}
},
{
_index:kpi,
_type:mroutes_by_lane
_id:JY9xNDxqSsajw76oMC2gxA,
_score:1.0,
_source:{
warehouse_id:107,
date 2013-04-08,
lane:M05,
routes:3056
{
_index:kpi,
_type:mroutes_by_lane,
_id:owVmGW9GT345_2Alfru2DB,
_score:1.0,
_source:{
warehouse_id:107,
date:2013-04-08,
lane :M03,
路线:5675
}
},
...
我想在ES中基本上运行与在SQL中一样的查询,所以我的结果会像(在当前的json中): M05 :7103,M03:9740
解决方案
在弹性搜索中,您可以使用术语统计facet :
{
查询:{
match_all:{}
},
facet :{
lane_routes_stats:{
terms_stats:{
key_field:lane,
value_field:routes ,
order:term
}
}
}
}
I'm fairly new to Elasticsearch. I'm trying to write a query that will group by a field and calculate a sum. In SQL, my query would look like this:
SELECT lane, SUM(routes) FROM lanes GROUP BY lane
I have this data that looks like this in ES:
{
"_index": "kpi",
"_type": "mroutes_by_lane",
"_id": "TUeWFEhnS9q1Ukb2QdZABg",
"_score": 1.0,
"_source": {
"warehouse_id": 107,
"date": "2013-04-08",
"lane": "M05",
"routes": 4047
}
},
{
"_index": "kpi",
"_type": "mroutes_by_lane",
"_id": "owVmGW9GT562_2Alfru2DA",
"_score": 1.0,
"_source": {
"warehouse_id": 107,
"date": "2013-04-08",
"lane": "M03",
"routes": 4065
}
},
{
"_index": "kpi",
"_type": "mroutes_by_lane",
"_id": "JY9xNDxqSsajw76oMC2gxA",
"_score": 1.0,
"_source": {
"warehouse_id": 107,
"date": "2013-04-08",
"lane": "M05",
"routes": 3056
}
},
{
"_index": "kpi",
"_type": "mroutes_by_lane",
"_id": "owVmGW9GT345_2Alfru2DB",
"_score": 1.0,
"_source": {
"warehouse_id": 107,
"date": "2013-04-08",
"lane": "M03",
"routes": 5675
}
},
...
I want to essentially run the same query in ES as I did in SQL, so that my result would be something like (in json of course): M05: 7103, M03: 9740
解决方案
In elasticsearch, you can achieve this by using terms stats facet:
{
"query" : {
"match_all" : { }
},
"facets" : {
"lane_routes_stats" : {
"terms_stats" : {
"key_field" : "lane",
"value_field" : "routes",
"order": "term"
}
}
}
}
这篇关于弹性搜索中的求和查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文