我正在使用 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 发出请求。
有人遇到过这个问题吗?
最佳答案
我忘记了一个重要的部分,响应正文是这样说的
{"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();
关于Elasticsearch POST/my-index/_count 错误 406 Not Acceptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72496582/