当前位置: 首页 > news >正文

es聚合-词条统计

es语句

---普通结构----"tags":{"type": "keyword","index": true},GET /knowledge_test/_search
{"size": 0,"aggs": {"tag_count": {"terms": {"field": "tags","size": 10000 // 设置足够	大的 size 值以包含所有可能的标签}}}
}--------------
nest结构:"inviteObjs": {"type": "nested","properties": {"email": {"type": "keyword","index": true},"userId": {"type": "keyword","index": true},"userName": {"type": "keyword","index": true}}},POST /knowledge/_search
{"size": 0,"query": {"term": {"authorId": "系统用户"}},"aggs": {"emails_count": {"nested": {"path": "inviteObjs"},"aggs": {"email_counts": {"terms": {"field": "inviteObjs.email","size": 10}}}}

对应api


----普通结构----
@Overridepublic List<String> queryTags(String type, Integer limit) {List<String> tags = new ArrayList<>();SearchRequest searchRequest = SearchRequest.of(s -> s.size(0).index(index).query(q -> q.term(t->t.field("type").value(type))).aggregations("tagsCount", a -> a.terms(t -> t.field("tags").size(limit))));SearchResponse<Object> response = EsSearchUtil.search(searchRequest, Object.class);Aggregate tagsCount = response.aggregations().get("tagsCount");if (tagsCount != null ){List<StringTermsBucket> hits = tagsCount.sterms().buckets().array();if (CollectionUtils.isEmpty(hits)){return tags;}for (StringTermsBucket hit : hits) {tags.add(hit.key().stringValue());}}return tags;}----nest----@Overridepublic List<InviteObjs> queryUsedEmail(Integer limit) {SearchRequest searchRequest = SearchRequest.of(s -> s.size(0).index(index).query(q -> q.term(t->t.field("authorId").value(UserUtil.getLoginUserId()))).aggregations("emailCount",a -> a.nested(n -> n.path("inviteObjs")).aggregations("email",at->at.terms(t->t.field("inviteObjs.email").size(limit)))));SearchResponse<Object> response = EsSearchUtil.search(searchRequest, Object.class);Aggregate emailCount = response.aggregations().get("emailCount");List<FieldValue> emails = new ArrayList<>();if (emailCount != null ){Aggregate emailAgg = emailCount.nested().aggregations().get("email");List<StringTermsBucket> hits = emailAgg.sterms().buckets().array();for (StringTermsBucket hit : hits) {String email = hit.key().stringValue();emails.add(FieldValue.of(email));}}
}

多层聚合统计

GET /knowledge/_search
{"size": 0,"range": {"timestamp": {"gte": "2015-01-01 00:00:00",  // 设置开始时间"lte": "2025-12-31 23:59:59"   // 设置结束时间}}},"aggs": {"by_type": {"terms": {"field": "type","size": 100  // 返回分类数量},"aggs": {"by_org": {"terms": {"field": "org","size": 50  // 返回分类数量}}}}}
}

对应代码

         BoolQuery.Builder boolBuilder = new BoolQuery.Builder();boolBuilder.must(m->m.term(t->t.field(SearchConstants.TYPE).value(type)));boolBuilder.filter(m->m.range(r->r.field("timestamp").from(query.getStartTime()).to(query.getEndTime())));SearchRequest request = SearchRequest.of(s -> s.size(0).index(index).query(q -> q.bool(boolBuilder.build())).aggregations(firstCountName, a -> a.terms(t -> t.field(firstType).size(1000)).aggregations(secondCountName, a1 -> a1.terms(t -> t.field(secondType).size(1000)))));
http://www.xdnf.cn/news/514459.html

相关文章:

  • 量子计算 | 量子密码学的挑战和机遇
  • LWIP的NETCONN接口
  • APP手机端测试覆盖点
  • 专业漏洞扫描机构如何助力企业保障安全并提升竞争力?
  • 【MySQL】库与表的操作
  • 力扣热题——数组的最小相等和
  • 关于 Web 漏洞原理与利用:1. SQL 注入(SQLi)
  • 基于FPGA的电子万年历系统开发,包含各模块testbench
  • ​Docker 网络
  • 前端三剑客之HTML
  • 深入解析Python中的Vector2d类:从基础实现到特殊方法的应用
  • nginx服务器实验
  • 23种设计模式解释+记忆
  • 虚幻引擎5-Unreal Engine笔记之`GameMode`、`关卡(Level)` 和 `关卡蓝图(Level Blueprint)`的关系
  • 快速上手SElinux
  • 第8章 常用实用类
  • 基于shardingsphere的分库分表方案
  • redis读写一致问题
  • Visual Studio已更新为17.14+集成deepseek实现高效编程
  • AI大模型(二)embedding模型调用后对产生的数据进行分析
  • 水平可见直线--上凸包(andrew算法
  • 【嵙大o】C++作业合集
  • 不同版本 Linux 系统账号操作指令 ——rtkit 账号删除、普通账号的创建 / 删除 / 权限修改超详细大全
  • 如何在 Windows 11 或 10 上安装 Amazon Corretto
  • Ubuntu 20.04 报错记录: Matplotlib 无法使用 OpenCV 的 libqxcb.so
  • O2O电商变现:线上线下相互导流——基于定制开发开源AI智能名片S2B2C商城小程序的研究
  • Python蓝色飘雪
  • Linux云计算训练营笔记day10(MySQL数据库)
  • Java虚拟机 - JVM与Java体系结构
  • MyBatis 核心技术详解:从连接池到多表查询