基于Spring Data ElasticSearch 4.x的方法序列化(不使用Jackson)

huangapple go评论83阅读模式
英文:

Method based serialization with Spring Data ElasticSearch 4.x (without Jackson)

问题

我们正在运行一个Java的Spring应用程序,该应用程序使用Spring Data Elasticsearch将数据索引到ElasticSearch中。最近,我的团队从3.x版本升级到了4.x版本。根据https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch.mapping中的说明,这次升级移除了Jackson映射器。该文档包含以下引用:

使用MappingElasticsearchConverter现在涵盖了所有这些情况。

然而,我还没有找到解决以下问题的方法。我们过去通常通过在模型中拥有对字段进行逻辑处理的方法来构建ElasticSearch中的某些字段。例如:

public class Person {
  private String firstName;
  private String lastName;

  String getFullName(){
    return firstName  + " " + lastName;
  }
}

在3.x版本中,getFullName() 方法会被Jackson默认序列化,从而在ElasticSearch索引中生成一个 'fullName' 字段。但在4.x版本中,这个信息被省略了,我们的索引中不再有 'fullName' 字段。4.x版本中的注解似乎只支持字段的序列化选项,而不支持方法。Jackson的注解(例如@JsonInclude)在这种情况下显然不再起作用。

在使用Spring Data ElasticSearch 4.x版本中,我该如何将方法的结果映射到ElasticSearch中的字段呢?

英文:

We are running a Java, Spring application which indexes to ElasticSearch, using Spring Data Elasticsearch. Recently my team has moved from version 3.x to 4.x. This upgrade removes the Jackson mapper according to https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch.mapping This document contains the following quote:
> Using the MappingElasticsearchConverter now covers all these cases.

Yet, I have not found a way to resolve the following. We used to build up certain fields in ElasticSearch by having a method in our models that contain logical functions on the fields of this model. For example:

public class Person {
  private String firstName;
  private String lastName;

  String getFullName(){
    return firstName  + " " + lastName;
  }
}

Using version 3.x the getFullName() method would be serialized as default by Jackson, filling our ElasticSearch index with a 'fullName' field. In version 4.x this information is left out from our index. The annotations in 4.x seem to be only supporting serialization options for fields, not methods. Jackson annotations (@JsonInclude for example) obviously do not work here anymore.

How would I be able to map method results to a field in ElasticSearch with Spring Data ElasticSearch 4.x ?

答案1

得分: 1

你可以通过在方法上添加带有PROPERTY参数的@AccessType注解来实现此操作:

import org.springframework.data.annotation.AccessType;

@AccessType(AccessType.Type.PROPERTY)
String getFullName(){
  return firstName  + " " + lastName;
}

请注意,Spring Data Elasticsearch 还需要一个 setter 方法,因此您需要定义一个空方法:

void setFullName(String ignore){}

如果您对Elasticsearch为此文本属性提供的默认映射满意,那就是全部。

我认为应该可以在没有 setter 的情况下使用这个方法,而且除此之外,您还可以在方法上使用@Field注解,以便为这些属性获得适当的映射。

创建了一个 Jira 问题 来处理这个。

编辑于 2022 年 10 月 1 日:

此功能已经实现,将在接下来的版本(5.0.RC)中提供。

英文:

You can achieve this by adding the @AccessType annotation with the PROPERTY parameter to the method:

import org.springframework.data.annotation.AccessType;

@AccessType(AccessType.Type.PROPERTY)
String getFullName(){
  return firstName  + " " + lastName;
}

Please note, that Spring Data Elasticsearch needs a setter as well, so you will need to define an empty method:

void setFullName(String ignore){}

If you are good with the default mappings that Elasticsearch will provide for this text property, that's it.

I think it should be possible to use this without a setter and besides that, it should be possible to put the @Field annotation on the method to be able to have a proper mappings for such properties.

I created a Jira issue for this.

Edit 1.10.2022:

This is implementewd now and will be available in the next version (5.0.RC)

huangapple
  • 本文由 发表于 2020年8月27日 19:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63615297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定