You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用spring-boot-starter-data-elasticsearch对接OpenSearch报错求助

解决Spring Data Elasticsearch连接OpenSearch时的X-Elastic-Product头缺失错误

错误原因

Spring Data Elasticsearch(对应Spring Boot 2.7.18的版本为4.4.x)默认会校验响应中的X-Elastic-Product头,而OpenSearch作为Elasticsearch的分支,不会返回该头,因此触发了这个校验错误。

解决方案

方案一:继续使用Spring Data Elasticsearch,跳过产品头校验

Spring Data Elasticsearch 4.4.x已支持兼容OpenSearch,只需在客户端配置中添加跳过产品头校验的设置即可:

修改你的OpensearchConfig类,在ClientConfiguration构建时添加.disableElasticsearchProductHeader():

@Configuration
class OpensearchConfig: ReactiveElasticsearchConfiguration() {
    val httpHeaders = HttpHeaders()
    init {
        httpHeaders.add("Content-type", "application/json")
    }

    override fun clientConfiguration(): ClientConfiguration {
        val config = ClientConfiguration.builder()
            .connectedTo("localhost:9200")
            .usingSsl()
            .withDefaultHeaders(httpHeaders)
            .withBasicAuth("admin", "admin")
            .disableElasticsearchProductHeader() // 新增该行,跳过产品头校验
            .build()

        return config
    }
}

或者通过配置文件(application.properties/yaml)添加全局设置:

spring.data.elasticsearch.client.disable-elasticsearch-product-header=true

方案二:改用OpenSearch官方客户端

如果需要深度适配OpenSearch的专属特性(如Security插件、索引状态管理等),建议直接使用OpenSearch官方的Rest客户端:

  1. 调整依赖,排除Spring Data Elasticsearch中的Elasticsearch客户端,引入OpenSearch客户端:
<!-- Maven示例 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.opensearch.client</groupId>
    <artifactId>opensearch-rest-high-level-client</artifactId>
    <version>2.6.0</version> <!-- 与你的OpenSearch版本保持一致 -->
</dependency>
  1. 配置OpenSearch客户端:
@Configuration
class OpensearchConfig {
    @Bean
    fun opensearchRestClient(): RestHighLevelClient {
        val credentialsProvider = BasicCredentialsProvider()
        credentialsProvider.setCredentials(
            AuthScope.ANY,
            UsernamePasswordCredentials("admin", "admin")
        )

        return RestHighLevelClient(
            RestClient.builder(HttpHost("localhost", 9200, "https"))
                .setHttpClientConfigurer { httpClientBuilder ->
                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                    // 测试环境可忽略SSL证书校验,生产环境请使用合法证书
                    val sslContext = SSLContextBuilder.create()
                        .loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
                        .build()
                    httpClientBuilder.setSSLContext(sslContext)
                    httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                }
        )
    }
}

总结

  • 若想保留Spring Data Elasticsearch的Repository、Reactive操作等特性,方案一最便捷,仅需添加一行配置即可解决当前错误。
  • 若需使用OpenSearch专属功能,方案二更适配长期需求。

内容的提问来源于stack exchange,提问作者Каляшов Григорий

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:13:16