package com.yuchen.data.config; import org.elasticsearch.client.Client; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; /** * @author l * <p> * ElasticsearchTemplate初始化时,持久化了一个ResultsMapper对象, * 该对象中的EntityMapper负责对象数据的json化等工作, * 而EntityMapper实现有两, * DefaultEntityMapper和ElasticsearchEntityMapper(从3.2版本后引入), * 而平台初始化默认用的是DefaultEntityMapper实现。 * elasticsearch版本从3.2以后版本才加入了@Field的name属性,引入自带的字段别名映射能力, * 而ElasticsearchEntityMapper正是用于解决该问题,因此,需要自定义注入ElasticsearchTemplate的bean对象 */ @Configuration public class ElasticsearchConfig { @Bean public ElasticsearchTemplate elasticsearchTemplate(Client client, ElasticsearchConverter converter) { try { return new ElasticsearchTemplate(client, new ElasticsearchEntityMapper(converter.getMappingContext(), null)); } catch (Exception ex) { throw new IllegalStateException(ex); } } }
elasticsearch 升级 转换下划线
最新推荐文章于 2023-02-21 02:43:48 发布
本文介绍了在Elasticsearch 3.2版本及以上,由于新增了@Field的name属性以支持字段别名映射,因此需要自定义配置ElasticsearchTemplate来使用ElasticsearchEntityMapper,确保数据转换的正确性。通过在@Configuration注解的ElasticsearchConfig类中创建ElasticsearchTemplate的bean,实现了对DefaultEntityMapper的替换。
摘要由CSDN通过智能技术生成