Elasticsearch根据查询条件求和sum
一、概述
现有news表,数据存放在Elasticsearch中。需要根据关键字查询之后,sum计算点击数。
数据如下:
{"content":"变异毒株在国内首次出现社区传播","hits":468} {"content":"昆明市委书记:做好象群进主城准备","hits":489} {"content":"吸入式新冠疫苗正在申请紧急使用","hits":476}
注意:hits表示点击数。
二、效果演示
环境说明
操作系统:centos 7.6
ip地址:192.168.7.160
Elasticsearch版本:7.10.1
初始化数据
1. 创建索引news
进入linux系统,手动执行
curl -XPUT http://localhost:9200/news
2. 创建一个映射
curl -XPOST http://localhost:9200/news/_mapping -H 'Content-Type:application/json' -d' { "properties": { "content": {"type": "text"}, "hits": {"type": "long"} } }'
3. 索引加入一些文档
curl -XPOST http://localhost:9200/news/_create/1 -H 'Content-Type:application/json' -d' {"content":"变异毒株在国内首次出现社区传播","hits":468} ' curl -XPOST http://localhost:9200/news/_create/2 -H 'Content-Type:application/json' -d' {"content":"昆明市委书记:做好象群进主城准备","hits":489} ' curl -XPOST http://localhost:9200/news/_create/3 -H 'Content-Type:application/json' -d' {"content":"吸入式新冠疫苗正在申请紧急使用","hits":476} '
4. 查询数据
使用postman工具发送GET请求,url地址:http://192.168.7.160:9200/news/_search
返回数据

{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "news", "_type": "_doc", "_id": "1", "_score": 1, "_source": { "content": "变异毒株在国内首次出现社区传播", "hits": 468 } }, { "_index": "news", "_type": "_doc", "_id": "2", "_score": 1, "_source": { "content": "昆明市委书记:做好象群进主城准备", "hits": 489 } }, { "_index": "news", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "content": "吸入式新冠疫苗正在申请紧急使用", "hits": 476 } } ] } }
可以看到数据有3条
sum查询
使用postman工具发送POST请求,url地址:http://192.168.7.160:9200/news/_search
body参数为:
{ "query": { "match" : { "content" : "在" } }, "aggs" : { "total_hits" : { "sum" : { "field" : "hits" } } } }
参数说明:
查询content内容中,包含"在"的关键字,并sum计算点击数。
关于aggs下文会有详细说明。
aggs 聚合的模板
当query和aggs一起存在时,会先执行query的主查询,主查询query执行完后会搜出一批结果,而这些结果才会被拿去aggs拿去做聚合
另外要注意aggs后面会先接一层自定义的这个聚合的名字,然后才是接上要使用的聚合桶
如果有些情况不在意查询结果是什麽,而只在意aggs的结果,可以把size设为0,如此可以让返回的hits结果集是0,加快返回的速度
一个aggs裡可以有很多个聚合,每个聚合彼此间都是独立的,因此可以一个聚合拿来统计数量、一个聚合拿来分析数据、一个聚合拿来计算标准差...,让一次搜索就可以把想要做的事情一次做完
此例只定义了1个聚合total_hits,使用sum计算hits字段。
返回结果:
{ "took": 3, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.4700036, "hits": [ { "_index": "news", "_type": "_doc", "_id": "1", "_score": 0.4700036, "_source": { "content": "变异毒株在国内首次出现社区传播", "hits": 468 } }, { "_index": "news", "_type": "_doc", "_id": "3", "_score": 0.4700036, "_source": { "content": "吸入式新冠疫苗正在申请紧急使用", "hits": 476 } } ] }, "aggregations": { "total_hits": { "value": 944 } } }
注意:
aggregations 里面已经返回了sum结果,总的点击次数为944。可以计算一下468+476=944
本文参考链接:
https://blog.csdn.net/weixin_40341116/article/details/81173016
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2020-07-06 Rancher2.4.3 Rest API修改镜像地址
2018-07-06 python 全栈开发,Day78(Django组件-forms组件)