Not Auto-generating id for Long type but for String type field in Spring Data Elasticsearch

Viewed 7393

I am using spring boot application of version 1.5.6.RELEASE and spring-boot-starter-data-elasticsearch of the same version.

I have a model Greeting as like :

@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{
    @Id
    private Long id;
    private String username;
    // Getter, Setter and constructor added here
}

My controller and service classes are same for the below cases without the type of id.

When I have sent the below post request:

curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings

It replies:

{"id":null,"username":"sunkuet02","message":"this is test"}

Then I have changed the Greeting class changing the type of id to String.

@Document(indexName = "index", type = "greetings")
public class Greeting implements Serializable{    
    @Id
    private String id;    
    private String username;
    // Getter, Setter and constructor added here
}

Clean, build and send the same post request:

curl -H "Content-Type: application/json" -X POST -d '{"username":"sunkuet02","message": "this is test"}' http://localhost:8080/api/greetings

And got the below response :

{"id":"AV2cq2OXcuirs1TrVgG6","username":"sunkuet02","message":"this is test"}

The scenario is : When the type of id field is Long then it doesn't generate id automatically but if the type is String then it generates id automatically.

My questions are:

  • What is the actual reason ?
  • Does spring-data-elasticsarch always uses id field of type String ?
  • Is there any way in configuration to use id of type Long without adding any annotations.
2 Answers
Related