如何在 Elasticsearch Java API 客户端中禁用兼容性 header?

How to disable compatibility header in Elasticsearch Java API Client?

我正在尝试配置我的应用程序以使用 Java API Client v7.16.3 与 Elastcisearch 服务器 v7.7 进行交互。但是当我尝试索引新文档时,出现以下错误:

{"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=7] is not supported","status":406}

据我了解,原因是 compatibility Content-type header 其中值包含 compatible-with=7。 Elasticsearch v7.7 不支持此 content-type(而 Elasticsearch v7.15 可以正常使用)。这是我的配置:

    fun configElasticsearchRestClient(
        host: String,
        username: String,
        password: String
    ): ElasticsearchClient {
        val elasticsearchRestClient =  RestClient.builder(HttpHost.create(host))
            .build()
        val transport: ElasticsearchTransport = RestClientTransport(
            elasticsearchRestClient,
            JacksonJsonpMapper()
        )
        return ElasticsearchClient(transport)
    }

这里是索引方法:

    fun index(document: SomeDocument) {
        val indexRequest = IndexRequest.Builder<ObjectNode>()
            .index("some-index")
            .document(document)
            .id(document.getId())
            .version(document.getVersion())
            .versionType(External)
            .build()

        elasticsearchClient.index(indexRequest)
    }

是否可以为 Java API 客户端禁用兼容性 headers? 或者还有其他方法可以使用 Java API Elasticsearch 服务器 v7.7 客户端吗?

您应该始终使用与服务器版本兼容的客户端库版本。在这种情况下,您使用的是最新版本的客户端库(即 7.16.3)和较旧版本的服务器(即 7.7),您不应该这样做,或者您应该 expect some backward incompatibility issues

The client must have the same major version (e.g. 2.x, or 5.x) as the nodes in the cluster. Clients may connect to clusters which have a different minor version (e.g. 2.3.x) but it is possible that new functionality may not be supported. Ideally, the client should have the same version as the cluster.

如果您改用 Java client 7.7,就不会有任何问题。

另外,从最新的Java Client source code, the compatible-with value is hardcoded into the HTTP header value. If a header is found to not have that compatibility check, it is modified automatically可以看出要包含,所以没办法关掉。