Home > database >  Elasticsearch POST /my-index/_count error 406 Not Acceptable
Elasticsearch POST /my-index/_count error 406 Not Acceptable

Time:06-05

I'm using elasticsearch-java-client 7.17.4 to make my a count request to AWS Elasticsearch server like this follow code

elasticsearchClient.count(s -> s
    .index("my-index")
).count();

However the follow exception was happening

Caused by: org.elasticsearch.client.ResponseException: method [POST], 
host [https://my-host], URI [/my-index/_count], status line [HTTP/1.1 406 Not Acceptable]
{"error":"Content-Type header [application/vnd.elasticsearch json; compatible-with=8] is not supported","status":406}

Looking _count api at elasticsearch RestAPI reference sound like strange because the http method is GET, but elasticsearch-java make a request with POST.

Somebody had this issue?

CodePudding user response:

I forgot a important part, the response body are saying

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

Solution

RestClient restClient = RestClient
    .builder(new HttpHost(url, port, scheme))
    .setDefaultHeaders(new Header[]{
        new BasicHeader("Content-type", "application/json")
    })
    .build();

CodePudding user response:

Try updating the Java Client API to version 8.2.2. I tested with Java Client API v8.2.2. and ElasticSearch v7.17.1 and it worked.

  • Related