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

RestHighLevelClient 7.17.4兼容ES 8.5.1时include_type_name参数报错

解决RestHighLevelClient 7.17.x兼容ES 8.5.x时include_type_name参数错误的问题

问题原因

7.17.4版本的RestHighLevelClient开启setApiCompatibilityMode(true)后,部分索引操作API(比如GetIndexRequest)仍会自动添加已被ES 8.x彻底移除的include_type_name参数,导致请求被ES拒绝。

解决方案

方案一:通过请求拦截器手动移除参数

在RestClient构建时添加一个请求拦截器,拦截所有请求并移除include_type_name参数:

@Configuration
public class ElasticsearchConfig {

    @Value("${hs360.services.watchlist.mgmt.elasticsearch.host}")
    private String elsHost;

    @Value("${hs360.services.watchlist.mgmt.elasticsearch.port}")
    private int elsRestPort;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient sourceClient() {
        RestClient restClient = RestClient.builder(new HttpHost(elsHost, elsRestPort))
                .setRequestConfigCallback(r -> r.setConnectTimeout(60000).setSocketTimeout(90000))
                .addInterceptorLast((HttpRequestInterceptor) (request, context) -> {
                    String originalUri = request.getRequestLine().getUri();
                    if (originalUri.contains("include_type_name")) {
                        // 移除include_type_name参数及可能的前后冗余符号
                        String cleanedUri = originalUri.replaceAll("(\\?|&)include_type_name=[^&]*", "")
                                .replaceAll("^&", "")
                                .replaceAll("&$", "");
                        // 替换请求URI
                        request.setRequestLine(new CustomHttpRequestLine(
                                request.getRequestLine().getMethod(),
                                cleanedUri,
                                request.getRequestLine().getProtocolVersion()
                        ));
                    }
                })
                .build();

        return new RestHighLevelClientBuilder(restClient)
                .setApiCompatibilityMode(true)
                .build();
    }

    // 自定义HttpRequestLine实现,替换不可变的默认实现
    private static class CustomHttpRequestLine implements org.apache.http.RequestLine {
        private final String method;
        private final String uri;
        private final org.apache.http.ProtocolVersion protocolVersion;

        public CustomHttpRequestLine(String method, String uri, org.apache.http.ProtocolVersion protocolVersion) {
            this.method = method;
            this.uri = uri;
            this.protocolVersion = protocolVersion;
        }

        @Override
        public String getMethod() { return method; }

        @Override
        public String getUri() { return uri; }

        @Override
        public org.apache.http.ProtocolVersion getProtocolVersion() { return protocolVersion; }
    }
}

方案二:升级RestHighLevelClient到7.17.x最新版本

Elastic官方在7.17的后续小版本(如7.17.18)中修复了兼容模式下部分API仍携带include_type_name参数的问题。升级后无需额外配置,仅保持setApiCompatibilityMode(true)即可正常对接ES 8.x。

注意:升级时需确保Spring Boot版本与ES客户端版本适配,Spring Boot 2.7.x系列与ES 7.17.x系列兼容性良好。

内容的提问来源于stack exchange,提问作者Tomas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 20:55:09