springboot集成elasticsearch7的过程中日期问题

https://www.jianshu.com/p/697a2232938d

springboot集成elasticsearch7中Instant日期类型问题

**

1 springboot集成elasticsearch7的过程中,遇到了以下问题:

**

Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {MonthOfYear=8, YearOfEra=2020, DayOfMonth=13},ISO resolved to 07:36:42 of type java.time.format.Parsed
    at java.time.Instant.from(Instant.java:378)
    ... 192 common frames omitted
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
    at java.time.format.Parsed.getLong(Parsed.java:203)
    at java.time.Instant.from(Instant.java:373)
    ... 192 common frames omitted

2 实体类中有Instant类型的属性:

   @Field(type = FieldType.Date,format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
   private Instant createdDate;

3 elaticsearch中配置的mapping:

"createdDate": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
    }

4 查阅相关文档

3.1 spring data elasticsearch
image.png
3.2 elasticsearch官网
image.png

image.png

5 解决办法

将java和elasticsearch中的日期格式统一改为uuuu-MM-dd’T’HH:mm:ss.SSSX

        "createdDate": {
            "type": "date",
            "format": "uuuu-MM-dd'T'HH:mm:ss.SSSX"
        }
@Field(type = FieldType.Date,format = DateFormat.custom, pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSX")
private Instant createdDate;

参考:1.https://docs.spring.io/spring-data/elasticsearch/docs/4.0.3.RELEASE/reference/html/#elasticsearch.mapping.meta-model.rules
2.https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot集成Elasticsearch 7需要进行以下步骤: 1. 添加Elasticsearch依赖 在pom.xml文件添加以下依赖: ``` <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.0.0</version> </dependency> ``` 2. 创建Elasticsearch配置文件 在application.yml或application.properties文件添加以下配置: ``` spring.data.elasticsearch.cluster-nodes: localhost:9200 spring.data.elasticsearch.cluster-name: elasticsearch ``` 其,`spring.data.elasticsearch.cluster-nodes`指定Elasticsearch的主机名和端口号,`spring.data.elasticsearch.cluster-name`指定集群名称。 3. 创建Elasticsearch客户端 可以使用以下代码创建Elasticsearch客户端: ``` @Configuration public class ElasticSearchConfig { @Bean public RestHighLevelClient restHighLevelClient() { RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; } } ``` 4. 创建Elasticsearch存储库 可以使用Spring Data Elasticsearch来创建Elasticsearch存储库。在创建存储库时,需要继承`ElasticsearchRepository`接口并指定实体类和实体类的ID类型,例如: ``` @Repository public interface BookRepository extends ElasticsearchRepository<Book, Long> { } ``` 其,`Book`是实体类,`Long`是实体类的ID类型。 5. 在应用程序使用Elasticsearch存储库 现在可以在应用程序使用Elasticsearch存储库来执行CRUD操作,例如: ``` @Service public class BookService { @Autowired private BookRepository bookRepository; public Book save(Book book) { return bookRepository.save(book); } public void delete(Book book) { bookRepository.delete(book); } public Optional<Book> findById(Long id) { return bookRepository.findById(id); } public Iterable<Book> findAll() { return bookRepository.findAll(); } // 其他自定义方法 } ``` 以上就是Spring Boot集成Elasticsearch 7的基本步骤。当然,在实际开发,还需要根据具体需求进行一些配置和定制化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值