ElasticsearchClient 查询报UnrecognizedPropertyException: Unrecognized field “_class“

在使用ElasticsearchClient进行查询时遇到UnrecognizedPropertyException,问题源于默认配置文件中的 '_class' 字段。通过深入源码分析,发现系统使用了预设的配置而非自定义设置。为解决问题,需要重写客户端注入方式,简单修改现有代码,即可避免该异常。
摘要由CSDN通过智能技术生成

背景


项目使用的 spring data Elasticsearch 5.0.1 ,开始是多模块使用Jackson解析正常,在使用注解配置ES链接之后,就出现 UnrecognizedPropertyException: Unrecognized field "_class"错误。
肯定是没有使用到注入的ObjectMapper,查询半天都没有找到原因,chatGPT也问了,无果。
只有自己跟与源码看了。

@Configuration
public class ElasticsearchConfig extends ElasticsearchConfiguration {
   

    @Value("${spring.elasticsearch.rest.uris}")
    private String elasticsearchRestUri;
    @Autowired
    ObjectMapper objectMapper;


    @Override
    public ClientConfiguration clientConfiguration() {
   
        ClientConfiguration build = ClientConfiguration.builder()
                .connectedTo(elasticsearchRestUri)
                .withBasicAuth("elastic", "QLVGBDR70Im48XPalC")
                .build();
        return build;
    } 
}

这里使用的是ElasticsearchConfiguration,进入里面可以看到默认是使用的

public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
   

	/**
	 * Provides the {@link ElasticsearchClient} to be used.
	 *
	 * @param restClient the low level RestClient to use
	 * @return ElasticsearchClient instance
	 */
	@Bean
	public ElasticsearchClient elasticsearchClient(RestClient restClient) {
   

		Assert.notNull(restClient, "restClient must not be null");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值