Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch POST /my-index/_count error 406 Not Acceptable

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?

like image 344
Bruce Avatar asked Oct 18 '25 02:10

Bruce


1 Answers

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();
like image 175
Bruce Avatar answered Oct 20 '25 15:10

Bruce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!