如何使用Spring Data ElasticSearch为POJO定义ElasticSearch索引字段名称 [英] How to define ElasticSearch index field names for a POJO using Spring Data ElasticSearch
问题描述
我正在使用Spring Data ElasticSearch执行CRUD操作.默认情况下,当使用@Document注释的POJO被写入ElasticSearch索引时,索引字段名称与POJO的Java属性名称相同.如何将索引字段名称配置为不同?例如,对于此文档POJO:
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;
我该如何配置,以使ElasticSearch中的索引字段序列化为county_name而不是countyName?
how can I configure this so that the index field in ElasticSearch gets serialized as county_name instead of countyName ?
推荐答案
由于Spring-data-elasticsearch使用fastxml模块将POJO转换为json文档,因此您可以简单地使用
As Spring-data-elasticsearch uses fasterxml module to convert POJOs to json document, you could simply use,
@JsonProperty("country_name")
private String countryName
要在elasticsearch索引中获得其他字段名称.
To achieve a different field name in the elasticsearch index.
这篇关于如何使用Spring Data ElasticSearch为POJO定义ElasticSearch索引字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!