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

es的date类型字段按照原生格式进行分组聚合

PUT student2
{
"mappings": {"properties": {"name": {"type": "text","analyzer": "standard"  // 使用标准分析器,适合姓名字段},"birthday": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd"  // 日期格式,可以根据需求调整},"age": {"type": "integer"  // 年龄字段,使用整型},"gender": {"type": "keyword"  // 性别字段,使用 keyword 类型来避免分词},"email": {"type": "keyword"  // 使用 keyword 类型来存储不需要分词的电子邮件地址},"address": {"type": "text"  // 地址字段,适合使用全文本分词},"registration_date": {"type": "date","format": "yyyy-MM-dd'T'HH:mm:ss"  // 注册日期,包含时间},"is_active": {"type": "boolean"  // 是否激活标记,使用布尔类型}}
}
} 

插入数据

POST student1/_doc/1
{"name": "Alice","birthday": "2000","age": 24,"gender": "female","email": "alice@example.com","address": "123 Maple Street, Springfield","registration_date": "2022-08-01T09:30:00","is_active": true
}POST student1/_doc/2
{"name": "Bob","birthday": "1998-11","age": 26,"gender": "male","email": "bob@example.com","address": "456 Oak Avenue, Shelbyville","registration_date": "2023-01-10T14:00:00","is_active": true
}POST student1/_doc/3
{"name": "Charlie","birthday": "1995-03-25","age": 29,"gender": "male","email": "charlie@example.com","address": "789 Birch Road, Capital City","registration_date": "2021-07-15T11:00:00","is_active": false
}POST student1/_doc/4
{"name": "Diana","birthday": "1999","age": 25,"gender": "female","email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true
}

执行查询

GET student2/_search
{"size": 0,"aggs": {"birthday_by_original_format": {"terms": {"script": {"source": """if (params['_source']['birthday'] != null) {return params['_source']['birthday'].toString();}return null;"""},"size": 10}}}
}

得到结果:

{"took": 25,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"birthday_by_original_format": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "1995","doc_count": 1},{"key": "1998-11","doc_count": 1},{"key": "1999-07-20","doc_count": 1},{"key": "2000-05-15","doc_count": 1}]}}
}

查询语句写为java:

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;
import org.apache.http.HttpHost;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.Script;import java.io.IOException;
import java.util.Collections;
import java.util.List;public class ElasticsearchAggregationExample {public static void main(String[] args) {// 创建 Elasticsearch 客户端RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))  // 替换为你的 Elasticsearch 地址);try {// 构建查询请求SearchRequest searchRequest = new SearchRequest("student2");  // 替换为你的索引名称SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 构建聚合String scriptSource = "if (params['_source']['birthday'] != null) { " +"  return params['_source']['birthday'].toString(); " +"} " +"return null;";TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("birthday_by_original_format").script(new Script(ScriptType.INLINE, "painless", scriptSource, Collections.emptyMap())).size(10);  // 设置返回的桶数量// 将聚合添加到查询中sourceBuilder.aggregation(aggregationBuilder);sourceBuilder.size(0);  // 不返回具体文档,只返回聚合结果searchRequest.source(sourceBuilder);// 执行查询SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);// 解析聚合结果Terms terms = searchResponse.getAggregations().get("birthday_by_original_format");List<? extends Terms.Bucket> buckets = terms.getBuckets();for (Terms.Bucket bucket : buckets) {String key = bucket.getKeyAsString();  // 获取聚合的 key(原始的 birthday 值)long docCount = bucket.getDocCount();  // 获取文档数量System.out.println("Key: " + key + ", Doc Count: " + docCount);}} catch (IOException e) {e.printStackTrace();} finally {// 关闭客户端try {client.close();} catch (IOException e) {e.printStackTrace();}}}
}

终极解决方案:
给mapping的所有date类型字段添加子类型

PUT /student3
{"mappings": {"properties": {"address": {"type": "text"},"age": {"type": "integer"},"birthday": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd","fields": {                "raw": {                "type": "keyword"}}},"email": {"type": "keyword"},"events": {"type": "nested","properties": {"event_date": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd","fields": {                "raw": {                "type": "keyword"}}},"event_type": {"type": "keyword"}}},"gender": {"type": "keyword"},"is_active": {"type": "boolean"},"name": {"type": "text","analyzer": "standard"},"registration_date": {"type": "date","format": "yyyy-MM-dd'T'HH:mm:ss","fields": {                "raw": {                "type": "keyword"}}}}}
}

插入数据:

POST student1/_doc/1
{"name": "Alice","birthday": "2000","age": 24,"gender": "female","email": "alice@example.com","address": "123 Maple Street, Springfield","registration_date": "2022-08-01T09:30:00","is_active": true,"events": [{"event_date": "2022","event_type": "graduation"},{"event_date": "2023","event_type": "birthday"}]
}POST student1/_doc/2
{"name": "Bob","birthday": "1998-11","age": 26,"gender": "male","email": "bob@example.com","address": "456 Oak Avenue, Shelbyville","registration_date": "2023-01-10T14:00:00","is_active": true,"events": [{"event_date": "2022-06","event_type": "meeting"}]
}POST student1/_doc/3
{"name": "Charlie","birthday": "1995-03-25","age": 29,"gender": "male","email": "charlie@example.com","address": "789 Birch Road, Capital City","registration_date": "2021-07-15T11:00:00","is_active": false,"events": [{"event_date": "2021-10-10","event_type": "conference"},{"event_date": "2022-02-20","event_type": "workshop"},{"event_date": "2023-04-05","event_type": "meeting"}]
}POST student1/_doc/4
{"name": "Diana","birthday": "1999-07-20","age": 25,"gender": "female","email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_date": "2022-11-10","event_type": "webinar"}]
}

根据嵌套子字段的date类型字段聚合

GET /student3_new/_search
{"size": 0,"aggs": {"nested_events": {"nested": {"path": "events"},"aggs": {"event_date_groups": {"terms": {"field": "events.event_date.raw", "size": 10}}}}}
}

聚合结果:

{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"nested_events": {"doc_count": 7,"event_date_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "2021-10-10","doc_count": 1},{"key": "2022","doc_count": 1},{"key": "2022-02-20","doc_count": 1},{"key": "2022-06","doc_count": 1},{"key": "2022-11-10","doc_count": 1},{"key": "2023","doc_count": 1},{"key": "2023-04-05","doc_count": 1}]}}}
}
http://www.lryc.cn/news/524893.html

相关文章:

  • 高频次UDP 小包丢包分析
  • 科目四考试内容
  • 2015 年 4 月多省(区、市)公务员录用考试 《申论》真题详解
  • 四、CSS效果
  • 全面评测 DOCA 开发环境下的 DPU:性能表现、机器学习与金融高频交易下的计算能力分析
  • 图论 八字码
  • OSI5GWIFI自组网协议层次对比
  • 北理新源监控平台都管理哪些数据
  • WPS不登录无法使用基本功能的解决方案
  • 车载软件架构 --- CP和AP作为中央计算平台的软件架构双核心
  • 【技巧】优雅的使用 pnpm+Monorepo 单体仓库构建一个高效、灵活的多项目架构
  • 【深度学习基础】多层感知机 | 权重衰减
  • 修改word的作者 最后一次保存者 总编辑时间 创建时间 最后一次保存的日期
  • 青少年编程与数学 02-007 PostgreSQL数据库应用 15课题、备份与还原
  • Flutter:自定义Tab切换,订单列表页tab,tab吸顶
  • SAS-proc sgplot绘图
  • 怎么使用python 调用高德地图api查询位置和导航?
  • pikachu靶场-敏感信息泄露概述
  • 使用ssh推送项目到github
  • SAP MRP运行出现例外消息怎么处理?例外消息的优先级、案例分享
  • 002-SpringBoot整合AI(Alibaba)
  • Java中如何安全地停止线程?
  • Apache Tomcat文件包含漏洞复现(详细教程)
  • 个人学习 - 什么是Vim?
  • Flink Gauss CDC:深度剖析存量与增量同步的创新设计
  • docker 部署.netcore应用优势在什么地方?
  • AIP-126 枚举
  • P3707 [SDOI2017] 相关分析 Solution
  • Android AutoMotive --CarService
  • K8S中Service详解(三)