Java elasticsearch spring-数据集不同索引字段名

Java elasticsearch spring-data set different index field name

我将 elasticsearch java-api 与 spring-data 结合使用,但在索引文档时遇到问题。 我想要索引字段的不同名称。这意味着与 java 代码中的不同:

域对象:

@Document(indexName = "testindex", type = "message")
public class MessageObject {

@Id
private String  unid;

@FieldNameInElasticIndex(value = "javaMessage")  // I want anything like that
private String message;

private String secondMessage;

private String thirdMessage;

...

getters & setters

...

}

接口:

public interface MessageObjectRepository extends ElasticsearchRepository<MessageObject, Long> {


}

服务:

@Component
public class MessageService {

    @Autowired
    private MessageObjectRepository repository;


    public void addRegistrationObject(MessageObject msg) {

        repository.save(msg);
    }
}

所以....

是否可以更改名称,使索引看起来像:

"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_type": "message",
        "_id": "00113B325ED357B7C1257E2D001D5B4B",
        "_score": 1,
        "_source": {
          "unid": "00113B325ED357B7C1257E2D001D5B4B",
          "javaMessage": "Hello",  // <--- this is what I want (javaMessage)
          "secondMessage": null,
          "thirdMessage": "Third",

而不是

"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_type": "message",
        "_id": "00113B325ED357B7C1257E2D001D5B4B",
        "_score": 1,
        "_source": {
          "unid": "00113B325ED357B7C1257E2D001D5B4B",
          "message": "Hello",  // <--- this is NOT what I want (java name: message)
          "secondMessage": null,
          "thirdMessage": "Third",

?

想法很简单,就是在字段上使用 @JsonProperty 注释,并给出要在 JSON 序列化期间使用的名称:

@JsonProperty("javaMessage")
private String message;