@CreatedDate annotation does not work with spring-data-elasticsearch

lsh

I'm using spring-boot-data-elasticsearch to persist the documents into elasticsearch.

Below is my Document Object for it.

@Builder
@Document(indexName = "testidx", createIndex = false)
public class Book {
    @Id
    private final String _id;

    @NotNull
    @CreatedDate
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time)
    private final Instant regTime;

    @NotNull
    @Field(type = FieldType.Text)
    private final String bookName;
}

However, when I saved the object into the ElasticSearchRepository, the regTime(which has to be created and saved automatically by @CreatedDate), has not been saved.

elasticRepository.save(
            Book.builder()
                .bookName("test")
                .build()
        );
GET /testidx/_search

{
        "_index" : "testidx",
        "_type" : "_doc",
        "_id" : "M3IS_XgBGpcXDVxetXXn",
        "_score" : 1.0,
        "_source" : {
          "_class" : "{test class Name}",
          "bookName" : "test",
        }
      }

Could you please let me know what I have been missed?

I'm working on the spring-boot 2.4.5 (and spring-boot-starter-data-elasticsearch 2.4.5 also) and ElasticSearch 7.12.0.

P.J.Meisch

Please check the documentation about auditing (https://docs.spring.io/spring-data/elasticsearch/docs/4.2.0/reference/html/#elasticsearch.auditing).

Your entity needs to implement the Persistable interface and needs to reimplement the isNew() method (see section 9.2.1). Note: I just saw that in the documented entity sample the @CreatedDate and @CreatedBy annotations are missing. I will fix the docs, the annotation is needed of course.

Then do you have @EnableElasticsearchAuditing on your Spring Boot application or any other configuration class? This is needed as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

@CreatedDate annotation does not work with mysql

Spring Data @CreatedDate annotation doesn't work for me

Does Spring Data JPA's @PersistenceConstructor annotation work in combination with Hibernate?

Spring @Schleduled annotation does not work

Simple Spring @CreatedDate annotation is not working for me

Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually

Spring Data fills @LastModifiedDate by not @CreatedDate

Spring Data Couchbase @CreatedDate not working

Spring Data Elasticsearch's @Field annotation not working

Annotation based Auditing for spring-data-elasticsearch?

How does the Spring @ResponseBody annotation work?

Spring validation: regex annotation does not work

How does the EnableAutoConfiguration spring annotation work?

Spring MVC JdbcTemplate Transactional Annotation does not work

Does Spring Data Elasticsearch support Amazon Elasticsearch?

the @target annotation does not work when i work with spring aop

Spring Controller: Custom annotation with combined Spring annotations does not work

Spring Data Elasticsearch setting annotation did not take effect

Spring Annotation @WebMvcTest does not work in an app that has Jpa repositories

Spring's value annotation does not work in constructor argument

Why does my @Before annotation not work in Spring app?

Spring data solr HttpSolrClient does not use core annotation from entity

How does the FetchMode work in Spring Data JPA

How does @CreatedBy work in Spring Data JPA?

Spring data facet does not work in Intellij 14

How does Spring data binding work on the background?

Spring Data (JPA) does not work with dates?

How does Spring Data JPA work internally

Spring JPA Data scanning for repository does not work