Search code examples
elasticsearchspring-dataspring-data-elasticsearch

Spring Elastic Search Custom Field names


I am new to Elastic Search and I am trying to implement it using Spring-data-elasticsearch.

I have fields with names such as "Transportation", "Telephone_Number" in our elastic search documents.

When I try to map my @Domain object fields with those, I don't get any data for those as I couldn't successfully map those fields.

Tried to use @Field, was disappointed as it didn't have 'name' property in it to map with custom field name.

Tried different variations of a GETTER function, none of those seem to be mapping to those fields.

I started wondering if there's something I'm missing here. How does a domain object field look like which should map to a filed called something like "Transportation" ?

Any help appreciated


Solution

  • You can use custom name. Spring Data ES use Jackson. So, you can use @JsonProperty("your_custom_name") to enable custom name in ES Mapping

    for example:

    @Document(indexName = "your_index_name", type = "your_type_name")
    public class YourEntity {
       ....
       @JsonProperty("my_transportation")
       @Field(type = FieldType.String, searchAnalyzer = "standard", indexAnalyzer = "standard", store = true) // just for example
       private String myTransportation;
       ....
    }
    

    Note: I'm sorry anyway, my english is bad.. :D