elasticsearch 多聚合查询

hersing 2017-08-03 05:50:06
原始es中数据格式:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [ {
"_index" : "demo",
"_type" : "information",
"_id" : "8cbadcec89a04f288b57a3296fee65f5",
"_score" : 1.0,
"_source" : {
"id" : "8cbadcec89a04f288b57a3296fee65f5",
"name" : "james",
"date" : 1501743474101,
"address" : "深圳福田",
"money" : null,
"money2" : 33.33
}
}, {
"_index" : "demo",
"_type" : "information",
"_id" : "b717309157b14fe7a66f0cf10669e542",
"_score" : 1.0,
"_source" : {
"id" : "b717309157b14fe7a66f0cf10669e542",
"name" : "jono",
"date" : 1501743474213,
"address" : "深圳罗湖",
"money" : null,
"money2" : 11.11
}
}, {
"_index" : "demo",
"_type" : "information",
"_id" : "8072a1f1c52240d8a031a43f703f2085",
"_score" : 1.0,
"_source" : {
"id" : "8072a1f1c52240d8a031a43f703f2085",
"name" : "jono",
"date" : 1501743473596,
"address" : "深圳南山",
"money" : null,
"money2" : 55.55
}
} ]
}
}


我现在想要通过name,id做group by,还要通过money2做sum操作,预想的查询结果应该为2条数据,但是最终的查询结果还是为3条数据,下面的是我的请求语句:
{
"fields": [
"id",
"name",
"money2"
],
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*"
}
}
}
},
"aggs": {
"nameCount": {
"terms": {
"field": "name"
},
"aggs": {
"idCount": {
"terms": {
"field": "id"
},
"aggs": {
"moneyAmount": {
"sum": {
"field": "money2"
}
}
}
}
}
}
}
}

我想的是es搜索引擎应该是支持这种查询的,但是具体的实现方式不太清楚,希望有老司机来解惑,网上看的大多数只是得到聚合后的单个结果,而我是想要得到结果集
...全文
1745 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Munger6 2018-10-16
  • 打赏
  • 举报
回复
楼主:你的查询结果是对的,因为你的id不同 当去除id匹配条件,将查询回来两条结果:jono 加总。
jjshouji 2017-10-21
  • 打赏
  • 举报
回复
是要这样的结果? select name,id,sum(money2) from demo group by name,id
六六六嗷 2017-09-11
  • 打赏
  • 举报
回复
把你聚合的结果贴上来看下
shpery 2017-08-07
  • 打赏
  • 举报
回复
帮顶....没用过。。。
hersing 2017-08-07
  • 打赏
  • 举报
回复
果然这个东西很冷门吗
hersing 2017-08-07
  • 打赏
  • 举报
回复
引用 4 楼 shpery 的回复:
帮顶....没用过。。。
hersing 2017-08-04
  • 打赏
  • 举报
回复
为何没人
hersing 2017-08-03
  • 打赏
  • 举报
回复
没人

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧