Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define ElasticSearch index field names for a POJO using Spring Data ElasticSearch

I'm using Spring Data ElasticSearch to perform CRUD operations. By default, when a POJO annotated with @Document gets written to an ElasticSearch index, the index field names are the same as the POJO's Java property names. How can I configure the index field names to be different ? For example, with this Document POJO:

@Document(indexName = "areas", type = "area")
public class Area {

    @Id
    private String id;
    private String countyName;
    private String postOfficeName;
    private String stateName;

how can I configure this so that the index field in ElasticSearch gets serialized as county_name instead of countyName ?

like image 589
Fabien Coppens Avatar asked Mar 03 '15 18:03

Fabien Coppens


1 Answers

As Spring-data-elasticsearch uses fasterxml module to convert POJOs to json document, you could simply use,

@JsonProperty("country_name")
private String countryName

To achieve a different field name in the elasticsearch index.

like image 183
techno shaft Avatar answered Oct 05 '22 03:10

techno shaft