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}]}}}
}
相关文章:
es的date类型字段按照原生格式进行分组聚合
PUT student2 { "mappings": {"properties": {"name": {"type": "text","analyzer": "standard" // 使用标准分析器,适合姓名字段},"birthday": {"type": "date&…...
高频次UDP 小包丢包分析
目录 背景测试方法测试结果case1: (经过多级交换机)case2: 长时测试(经过多级交换机)case3: 长时测试(设备直联)可能原因分析解决方法背景 UDP作为面向非连接的传输协议,并不能保证可靠交付。本文编写代码测试设备之间UDP小包传输的可靠性。 测试方法 发送侧基于豆包…...
科目四考试内容
一、考试内容 科目四考试主要包含以下五个方面的内容: 法律法规与规章制度:理解并掌握道路交通规则,涉及交通信号、标志、标线以及相关设施的运用。综合判断与案例分析:培养学员应对复杂交通情况的能力,学会识别违法…...
2015 年 4 月多省(区、市)公务员录用考试 《申论》真题详解
一)“给定资料1~2”反映了人们在过去的工作和生活方面形成的很多“惯例”或“习惯做法”正在悄然改变。请分析导致这种改变发生的主要原因。(20分) 一、给定资料 材料1: 互联网的日益普及和开发利用,不断为人…...
四、CSS效果
一、box-shadow box-shadow:在元素的框架上添加阴影效果 /* x 偏移量 | y 偏移量 | 阴影颜色 */ box-shadow: 60px -16px teal; /* x 偏移量 | y 偏移量 | 阴影模糊半径 | 阴影颜色 */ box-shadow: 10px 5px 5px black; /* x 偏移量 | y 偏移量 | 阴影模糊半径 | 阴影扩散半…...
全面评测 DOCA 开发环境下的 DPU:性能表现、机器学习与金融高频交易下的计算能力分析
本文介绍了我在 DOCA 开发环境下对 DPU 进行测评和计算能力测试的一些真实体验和记录。在测评过程中,我主要关注了 DPU 在高并发数据传输和深度学习场景下的表现,以及基本的系统性能指标,包括 CPU 计算、内存带宽、多线程/多进程能力和 I/O 性…...
图论 八字码
我们可能惊异于某些技巧。我们认为这个技巧真是巧妙啊。或者有人认为我依靠自己的直觉想出了这个表示方法。非常自豪。我认为假设是很小的时候,比如说小学初中,还是不错的。到高中大学,就有一些不成熟了。因为这实际上是一个竞技。很多东西前…...
OSI5GWIFI自组网协议层次对比
目录 5G网络5G与其他协议栈各层映射 5G网络 物理层 (PHY) 是 5G 基站协议架构的最底层,负责将数字数据转换为适合无线传输的信号,并将接收到的无线信号转换为数字数据。实现数据的编码、调制、多天线处理、资源映射等操作。涉及使用新的频段(…...
北理新源监控平台都管理哪些数据
北理新源信息科技有限公司(简称“北理新源”)依托北京理工大学电动车辆国家工程研究中心,建设和运营了“新能源汽车国家监测与管理平台”。该平台是国家级的新能源汽车数据监管平台,主要负责对新能源汽车的运行数据进行采集、监测…...
WPS不登录无法使用基本功能的解决方案
前言 WPS不登录无法使用基本功能的原因通常是为了同步数据、提供更多高级功能或满足软件授权要求。然而,一些用户可能出于隐私或便捷性的考虑,不愿意登录账号。在这种情况下,WPS可能会限制未登录用户的使用权限,导致工具栏变灰…...
车载软件架构 --- CP和AP作为中央计算平台的软件架构双核心
我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 简单,单纯,喜欢独处,独来独往,不易合同频过着接地气的生活…...
【技巧】优雅的使用 pnpm+Monorepo 单体仓库构建一个高效、灵活的多项目架构
单体仓库(Monorepo)搭建指南:从零开始 单体仓库(Monorepo)是一种将多个相关项目集中管理在一个仓库中的开发模式。它可以帮助开发者共享代码、统一配置,并简化依赖管理。本文将通过实际代码示例࿰…...
【深度学习基础】多层感知机 | 权重衰减
【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈PyTorch深度学习 ⌋ ⌋ ⌋ 深度学习 (DL, Deep Learning) 特指基于深层神经网络模型和方法的机器学习。它是在统计机器学习、人工神经网络等算法模型基础上,结合当代大数据和大算力的发展而发展出来的。深度学习最重…...
修改word的作者 最后一次保存者 总编辑时间 创建时间 最后一次保存的日期
作者: 1.打开word文件 2.点击左上角的文件 3.选项 4.用户信息 5.将用户信息中的 姓名改为你需要的名字 最后一次保存者 1.word重命名为.zip文件 2.docProps中有个core.xml 3.用记事本打开有个lastModifiedBy标签,将里面内容改为你需要的名字 总编辑时…...
青少年编程与数学 02-007 PostgreSQL数据库应用 15课题、备份与还原
青少年编程与数学 02-007 PostgreSQL数据库应用 15课题、备份与还原 一、数据库备份与还原二、PostgreSQL中操作数据库的备份与还原1. 使用pg_dump进行逻辑备份2. 使用pg_restore进行逻辑还原3. 使用pg_basebackup进行物理备份4. 还原物理备份注意事项 三、自动备份1. 使用pg_d…...
Flutter:自定义Tab切换,订单列表页tab,tab吸顶
1、自定义tab切换 view <Widget>[// 好评<Widget>[TDImage(assetUrl: assets/img/order4.png,width: 36.w,height: 36.w,),SizedBox(width: 10.w,),TextWidget.body(好评,size: 24.sp,color: controller.tabIndex 0 ? AppTheme.colorfff : AppTheme.color999,),]…...
SAS-proc sgplot绘图
1、绘图-直条图示例: 1.1 数据集 1.2 代码 proc sgplot data sashelp.cars;vbar origin / response msrp /* response:响应变量,Y轴 */stat mean /* stat:统计量,结果用均值呈现 */group type /* group&#…...
怎么使用python 调用高德地图api查询位置和导航?
环境: python 3.10 问题描述: 怎么使用python 调用高德地图api查询位置和导航? 解决方案: 要使用Python调用高德地图API查询位置和导航,需要先注册高德开发者账号并获取API Key。以下是基本步骤: 1. 注册高德开…...
pikachu靶场-敏感信息泄露概述
敏感信息泄露概述 由于后台人员的疏忽或者不当的设计,导致不应该被前端用户看到的数据被轻易的访问到。 比如: ---通过访问url下的目录,可以直接列出目录下的文件列表; ---输入错误的url参数后报错信息里面包含操作系统、中间件、开发语言的版…...
使用ssh推送项目到github
文章目录 1. 确保已生成 SSH 密钥2. 在 GitHub 上创建远程仓库3. 初始化本地项目4. 将本地项目与远程仓库关联5. 添加文件并提交补充:拉取远程修改(可选)6. 推送到 GitHub7. 完成总结 出现的问题解决方法:方法 1:允许合…...
docker详细操作--未完待续
docker介绍 docker官网: Docker:加速容器应用程序开发 harbor官网:Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台,用于将应用程序及其依赖项(如库、运行时环…...
QMC5883L的驱动
简介 本篇文章的代码已经上传到了github上面,开源代码 作为一个电子罗盘模块,我们可以通过I2C从中获取偏航角yaw,相对于六轴陀螺仪的yaw,qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...
(二)TensorRT-LLM | 模型导出(v0.20.0rc3)
0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述,后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作,其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...
Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...
Go 语言并发编程基础:无缓冲与有缓冲通道
在上一章节中,我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道,它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好࿰…...
Python Einops库:深度学习中的张量操作革命
Einops(爱因斯坦操作库)就像给张量操作戴上了一副"语义眼镜"——让你用人类能理解的方式告诉计算机如何操作多维数组。这个基于爱因斯坦求和约定的库,用类似自然语言的表达式替代了晦涩的API调用,彻底改变了深度学习工程…...
Qemu arm操作系统开发环境
使用qemu虚拟arm硬件比较合适。 步骤如下: 安装qemu apt install qemu-system安装aarch64-none-elf-gcc 需要手动下载,下载地址:https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x…...
LOOI机器人的技术实现解析:从手势识别到边缘检测
LOOI机器人作为一款创新的AI硬件产品,通过将智能手机转变为具有情感交互能力的桌面机器人,展示了前沿AI技术与传统硬件设计的完美结合。作为AI与玩具领域的专家,我将全面解析LOOI的技术实现架构,特别是其手势识别、物体识别和环境…...
