Elasticsearch POST /my-index/_count错误406不可接受

wtzytmuj  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(3)|浏览(240)

我使用elasticsearch-java-client 7.17.4向AWS Elasticsearch服务器发出计数请求,如下代码所示

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

字符串
但是,发生了以下异常

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}


_count api at elasticsearch RestAPI reference听起来很奇怪,因为http方法是GET,但elasticsearch-java用POST发出请求。
有人有这个问题吗?

ybzsozfc

ybzsozfc1#

我忘了一个重要的部分,响应机构都在说

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

字符串

解决方案

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

cu6pst1q

cu6pst1q2#

尝试将Java客户端API更新到版本8.2.2。我使用Java客户端API v8.2.2进行了测试。和ElasticSearch v7.17.1,它工作了。

pdtvr36n

pdtvr36n3#

我也遇到了同样的问题。然而,我的问题与elasticsearch的版本有关。我们有义务只使用7.10.2。我使用co.elastic.clients:elasticsearch-java:8.9.0scala org.scala-lang:scala-library:2.12.13的版本
在我修复了Content-type的问题之后,我又遇到了X-Elastic-Product的另一个问题。所以我必须做另一个修复来使用特定的elasticsearch版本。
这个答案帮助了我: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()

字符串

相关问题