elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
408 stars 237 forks source link

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

Closed LiuGuBiGu closed 4 months ago

LiuGuBiGu commented 5 months ago

Java API client version

elasticsearch-java: 8.10.0

Java version

java: 11

Elasticsearch Version

Elasticsearch: 7.10.2

Problem description

When I execute the following code:

public static void main(String[] args) throws IOException {
RestClientBuilder restClientBuilder = RestClient.builder(HttpHost.create(ValidationUtils.ensureNotNull("10.48.56.215:9200", "serverUrl")));
        RestClient restClient = restClientBuilder.build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
        client.indices().create((c) -> {
            return c.index("test11").mappings(getDefaultMappings(1536));
        });
}

static TypeMapping getDefaultMappings(int dimension) {
        Map<String, Property> properties = new HashMap(4);
        properties.put("text", Property.of((p) -> {
            return p.text(TextProperty.of((t) -> {
                return t;
            }));
        }));
        properties.put("vector", Property.of((p) -> {
            return p.denseVector(DenseVectorProperty.of((d) -> {
                return d.dims(dimension);
            }));
        }));
        return TypeMapping.of((c) -> {
            return c.properties(properties);
        });
    }

Error log:

Exception in thread "main" org.elasticsearch.client.ResponseException: method [PUT], host [http://10.48.56.215:8200], URI [/test11], status line [HTTP/1.1 406 Not Acceptable]
{"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported","status":406}
    at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:347)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:313)
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288)
    at co.elastic.clients.transport.rest_client.RestClientHttpClient.performRequest(RestClientHttpClient.java:91)
    at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:137)
    at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:266)
    at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:282)
    at com.qax.dayu.assistant.application.service.loader.DocLoadService.main(DocLoadService.java:163)

If executing the following code is good

RestClient restClient = RestClient
                .builder(new HttpHost("10.48.56.215", 8200))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader("Content-type", "application/vnd.elasticsearch+json; compatible-with=8"),
                        new BasicHeader("Accept", "application/vnd.elasticsearch+json; compatible-with=8")
                })
                .build();
        Request request = new Request("PUT","/test11");
        Response response = restClient.performRequest(request);
        System.out.println(response.toString());
l-trotta commented 5 months ago

Hello! As stated in the compatibility section of the Readme:

The Java client is forward compatible; meaning that the client supports communicating with greater or equal minor versions of Elasticsearch without breaking

Forcing the compatibility header ignores the error, but does not guarantee that everything will function properly, so I suggest aligning the java client version with the elasticsearch server version by using the same major version (the current latest is 8.12.2) for both.

Hope this helps!