当前位置: 首页 > 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)))));

相关文章:

  • Java学习手册:服务熔断与降级
  • Ubuntu 18.04设置静态IP的方法(图形化操作)
  • Spring-Beans的生命周期的介绍
  • nginx模块使用、过滤器模块以及handler模块
  • Linux 文件(1)
  • 用golang实现二叉搜索树(BST)
  • 飞帆控件:on_post_get 接口配置
  • YOLO12改进-模块-引入Channel Reduction Attention (CRA)模块 降低模型复杂度,提升复杂场景下的目标定位与分类精度
  • 处理金融数据,特别是股票指数数据,以计算和分析RSRS(相对强度指数)
  • OpenAI深夜发布Codex:AI编程里程碑式突破
  • 二:操作系统之进程的创建与终止
  • 基于区块链技术的智能汽车诊断与性能分析
  • Ansible模块——文件属性查看,文件或目录创建和属性修改
  • 量子计算 | 量子密码学的挑战和机遇
  • Docker配置容器开机自启或服务重启后自启
  • 【iOS】探索消息流程
  • PCB设计(十九)PCB设计中NPN/PNP选型策略
  • 【甲方安全建设】拉取镜像执行漏洞扫描教程
  • 智慧大楼综合信息化建设方案,技术解决方案(PPT)
  • 【YOLO(txt)格式转VOC(xml)格式数据集】以及【制作VOC格式数据集 】
  • 常州新型碳材料集群产值近二千亿,请看《浪尖周报》第24期
  • 南京艺术学院博导、雕塑家尹悟铭病逝,年仅45岁
  • 新疆多地市民拍到不明飞行物:几秒内加速消失,气象部门回应
  • 一条铺过11年时光的科学红毯,丈量上海科创的“长宽高”
  • 精品消费“精”在哪?多在体验上下功夫
  • 沃尔玛上财季净利下滑12%:关税带来成本压力,新财季价格涨幅将高于去年