elasticsearch 升级 转换下划线

本文介绍了在Elasticsearch 3.2版本及以上,由于新增了@Field的name属性以支持字段别名映射,因此需要自定义配置ElasticsearchTemplate来使用ElasticsearchEntityMapper,确保数据转换的正确性。通过在@Configuration注解的ElasticsearchConfig类中创建ElasticsearchTemplate的bean,实现了对DefaultEntityMapper的替换。
摘要由CSDN通过智能技术生成
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);
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值