4

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?

Bruce
  • 1,145
  • 1
  • 10
  • 16

3 Answers3

4

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();
Bruce
  • 1,145
  • 1
  • 10
  • 16
  • I saw this solution but as my versions were working I didn't recommend it. If yours is working now it's great too. – rabbitbr Jun 04 '22 at 14:46
0

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.

rabbitbr
  • 2,991
  • 2
  • 4
  • 17
  • I try to use 8.2.2 but same problem. My Elasticsearch Server is 7.10. It's strange the RestAPI reference expressly method GET but Java Client API make POST. – Bruce Jun 04 '22 at 12:45
  • Here code that prepare the request https://github.com/elastic/elasticsearch-java/blob/df8ba67f1f0471ddd4363a7c80e53ef23e305963/java-client/src/main/java/co/elastic/clients/elasticsearch/core/CountRequest.java#L625 – Bruce Jun 04 '22 at 12:50
  • It's my bad Andre. I forgot a important part from error. Thanks for help. – Bruce Jun 04 '22 at 13:29
0

I faced the same problem. However, my problem related to the version of the elasticsearch. We have some obligation to use only the 7.10.2. And I use the version of co.elastic.clients:elasticsearch-java:8.9.0 and scala org.scala-lang:scala-library:2.12.13

After I fixed the problem with Content-type, I got another problem with the X-Elastic-Product. So I must do another fix to work with a specific elasticsearch version.

This answer helps me: co.elastic.clients.transport.TransportException: [es/search] Missing [X-Elastic-Product] header


    val client: RestClient = RestClient
      .builder(HttpHost.create(s"$node"))
      .setHttpClientConfigCallback(httpClientConfigCallback =>
        httpClientConfigCallback
          .addInterceptorLast((response: HttpResponse, _: HttpContext) =>
            response.addHeader("X-Elastic-Product", "Elasticsearch")))
      .setDefaultHeaders(Array(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString)))
      .build()

Pavlo Chechehov
  • 390
  • 6
  • 17