如何使用Spring Data Elasticsearch为指定String字段实现自定义转换器
可以实现,你可以通过字段级专属转换器的方式实现需求,完全不会影响其他String类型字段,以下是具体实现方案:
方案1:使用AttributeConverter(推荐,最简实现)
Spring Data Elasticsearch 4.0+ 原生支持字段级转换器绑定,仅需两步即可完成:
1. 自定义双向字段转换器
import org.springframework.data.convert.AttributeConverter; // 仅处理restField字段的读写转换逻辑 public class RestFieldConverter implements AttributeConverter<String, String> { // 写入ES时触发:移除***字符 @Override public String convertToDatabaseColumn(String attribute) { if (attribute == null) { return null; } return attribute.replace("***", ""); } // 从ES读取时触发:追加***字符 @Override public String convertToEntityAttribute(String dbData) { if (dbData == null) { return null; } return dbData.concat("***"); } }
2. 绑定转换器到指定字段
仅在restField字段上添加@Convert注解,不会对其他String字段产生任何影响:
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.annotation.Id; import org.springframework.data.convert.Convert; @Document("example") class Example { @Id private String id; private String name; private String game; // 仅当前字段应用自定义转换器 @Convert(converter = RestFieldConverter.class) private String restField; // 省略getter、setter }
配置完成后直接调用Repository原生的save、findById方法即可,无需再手动处理字段值。
方案2:低版本兼容实现(3.x及以下版本)
如果使用的Spring Data Elasticsearch版本不支持AttributeConverter,可以通过自定义读写转换器实现:
1. 自定义读写转换器
import org.springframework.data.convert.ReadingConverter; import org.springframework.data.convert.WritingConverter; import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.core.convert.converter.Converter; // 写入ES时的转换器,仅处理Example类型 @WritingConverter public class ExampleWritingConverter implements Converter<Example, Document> { @Override public Document convert(Example source) { Document document = Document.create(); document.put("id", source.getId()); document.put("name", source.getName()); document.put("game", source.getGame()); // 单独处理restField字段 String restField = source.getRestField(); document.put("restField", restField == null ? null : restField.replace("***", "")); return document; } } // 读取ES数据时的转换器,仅处理Example类型 @ReadingConverter public class ExampleReadingConverter implements Converter<Document, Example> { @Override public Example convert(Document source) { Example example = new Example(); example.setId(source.getString("id")); example.setName(source.getString("name")); example.setGame(source.getString("game")); // 单独处理restField字段 String restField = source.getString("restField"); example.setRestField(restField == null ? null : restField.concat("***")); return example; } }
2. 注册转换器到ES配置
import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions; import java.util.Arrays; @Configuration public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { // 省略RestHighLevelClient等基础配置 @Override public ElasticsearchCustomConversions elasticsearchCustomConversions() { return new ElasticsearchCustomConversions(Arrays.asList( new ExampleReadingConverter(), new ExampleWritingConverter() )); } }
内容的提问来源于stack exchange,提问作者H Athukorala
相关产品推荐
相关产品推荐

