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

Elasticsearch 8.13.4 LocalDateTime类型转换问题

框架背景

springboot 3.3.1+elasticseach8.13.4+spring-data-elasticsearch5.3.1(其实只要用了springboot3.3.1 上下两个的版本都在里面绑死了)

问题描述

使用spring-data-elasticsearch操作es,当字段增加映射注解,其实如果是日期类型,你不加默认也给你映射成date了

@Field(type = FieldType.Date)

可以正常保存成功,但查询时会报错

org.springframework.data.elasticsearch.core.convert.ConversionException: Unable to convert value ‘2024-08-30’ to java.time.LocalDateTime for property ‘createTime’

通过kibana查看数据

数据查询发现保存的数据格式是 “2024-08-30”,导致读取时解析失败

解决方案使用自定义的转换器,我这里是将LocalDateTime保存为时间戳,读取的时候再转为LocalDateTime

以下是配置类(我是自定义了一个starer,所以用了@AutoConfiguration)

package cn.iocoder.centralstore.framework.es.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;/**** @Description: 处理 Elasticsearch 中 LocalDateTime 与时间戳之间的转换* 将 LocalDateTime 写入为时间戳,读取时将时间戳转换为 LocalDateTime。* @Author: TaoYuan* @Date: 2024/8/29*/
@AutoConfiguration
@Slf4j
public class CentralstoreLocalDateTimeConverter implements PropertyValueConverter {// 使用系统默认时区private static final ZoneId ZONE_ID = ZoneId.systemDefault();@Overridepublic Object write(Object value) {if (value instanceof LocalDateTime localDateTime) {Instant instant = localDateTime.atZone(ZONE_ID).toInstant();long timestamp = instant.toEpochMilli();log.info("将 LocalDateTime [{}] 转换为时间戳 [{}]", localDateTime, timestamp);return timestamp;} else {String errorMessage = String.format("写入操作接收到非 LocalDateTime 值: [%s], 类型: [%s]", value, value.getClass().getName());log.error(errorMessage);throw new IllegalStateException(errorMessage);}}@Overridepublic Object read(Object value) {if (value instanceof Long timestamp) {LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZONE_ID);log.info("将时间戳 [{}] 转换为 LocalDateTime [{}]", timestamp, localDateTime);return localDateTime;} else {String errorMessage = String.format("无法将值 '值: [%s], 类型: [%s] 解析为 LocalDateTime", value,value.getClass().getName());log.error(errorMessage);throw new IllegalStateException(errorMessage);}}
}

字段增加注解

@Field(type = FieldType.Date)
@ValueConverter(CentralstoreLocalDateTimeConverter.class)
private LocalDateTime createTime;```

至此,插入数据和查询数据LocalDateTime类型就搞定了。其实里面还有很多细节,但是大部分估计跟我一样,只想着找到解决方案,不去想为什么会这样。所以就懒得继续深讲了。

还有很多坑,比如使用雪花算法生成的Id是19位,存入es后后两位精度丢失,这个处理起来最简单的办法就是使用String类型的作为id。或者跟上面一样 增加一个Long类型的转换器,转为String ,读取的时候再转换为Long。这个我还没有尝试,只是目前改了一下id的数据类型。

到现在为止仍然没有整合完,还有一堆坑等着我,次奥!!!次奥!!!次奥!!!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • OpenCV小练习:人脸检测
  • [Linux]如何將A主機的docker image轉移到B主機,並在B主機中重新配置和執行該docker image?
  • C++(this指针/常函数与常对象/拷贝构造函数/赋值函数/静态成员/静态成员函数/单列模式)
  • JAVA中的元注解
  • 【nvm】解决问题: Could not retrieve https://nodejs.org/dist/index.json.
  • 学习 TagUI 踩过的坑
  • 防抖函数 debounce debouncePromise
  • 少走弯路,ESP32 读取Micro SD(TF)播放mp3的坑路历程。
  • ET6框架(七)Excel配置工具
  • 【C++标准模版库】模拟实现容器适配器:stack、queue、priority_queue(优先级队列)
  • 集成电路学习:什么是BLE低功耗蓝牙
  • [英语单词] feedback
  • 如何从零开始在 Vue 3 项目中引入 Element Plus
  • 逆波兰表达式求值
  • 安卓13 背光反向 亮度反向 android13 backlight reverse
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • 30秒的PHP代码片段(1)数组 - Array
  • android 一些 utils
  • C++11: atomic 头文件
  • crontab执行失败的多种原因
  • ES10 特性的完整指南
  • Fundebug计费标准解释:事件数是如何定义的?
  • mongo索引构建
  • MySQL用户中的%到底包不包括localhost?
  • tab.js分享及浏览器兼容性问题汇总
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 基于组件的设计工作流与界面抽象
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 前端攻城师
  • 区块链技术特点之去中心化特性
  • 如何在 Tornado 中实现 Middleware
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 微服务入门【系列视频课程】
  • 我的zsh配置, 2019最新方案
  • 写给高年级小学生看的《Bash 指南》
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (26)4.7 字符函数和字符串函数
  • (笔试题)合法字符串
  • (附源码)ssm高校实验室 毕业设计 800008
  • (六)激光线扫描-三维重建
  • (一)SvelteKit教程:hello world
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • ***利用Ms05002溢出找“肉鸡
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .FileZilla的使用和主动模式被动模式介绍
  • .net core 控制台应用程序读取配置文件app.config
  • .net core 连接数据库,通过数据库生成Modell
  • .NET Core 中插件式开发实现
  • .NET Framework 服务实现监控可观测性最佳实践
  • .Net OpenCVSharp生成灰度图和二值图
  • .NET的数据绑定
  • .net连接oracle数据库
  • @GetMapping和@RequestMapping的区别