ElasticSearch Spring-Data Date format always is long


When using spring-data to insert Elasticsearch document with Date type, I can’t get right date format, the date format always is Long.

here is the java code: Entity.java

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonProperty;

@Document(indexName = "entity-index", type = "entity-type")
public class Entity {
    private String id;

    @Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true, 
            format = DateFormat.custom, pattern = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'")
    private Date createDate;

    private String system;
    private double score;

    @Field(type = FieldType.Date, format = DateFormat.date_optional_time)
    @JsonProperty(value = "@timestamp")
    private Date updateDate;
    // omit setter and getter 

Here is the Test

public class EntityDAOTest {
    private ElasticsearchTemplate template;

    public void init() {

    public void testCreate() {
        Entity entity = new Entity();
        entity.setCreateDate(new DateTime(2015,05,27,0,0).toDate());
        entity.setUpdateDate(new DateTime(2015,05,27,0,0).toDate());
        IndexQuery query = new IndexQueryBuilder().withObject(entity).withId(entity.getId()).build();

I can get the mapping of the created entity:

   "entity-index": {
      "mappings": {
         "entity-type": {
            "properties": {
               "@timestamp": {
                  "type": "long"
               "createDate": {
                  "type": "date",
                  "store": true,
                  "format": "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"
               "id": {
                  "type": "string"
               "score": {
                  "type": "double"
               "system": {
                  "type": "string"
               "updateDate": {
                  "type": "date",
                  "format": "date_optional_time"

However, when I search it curl -X GET /entity-index/_search, I get the following document:

               "id": "5",
               "createDate": 1432656000000,
               "system": "systemC",
               "score": 5.7,
               "@timestamp": 1432656000000

and the Date Fields are all Long type, how can I get the date format : ‘2015-08-17T12:00:00.000’?


Your mapping is created correctly. The problem is more likely to come from the Jackson JSON serializer. You should try adding this annotation to your date fields: @JsonFormat (shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ").

There are also some alternative solutions that might better suit your case (i.e. creating a CustomDateSerializer, etc).

Answered By – Val

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published