Elasticsearch POST/my-index/_count 错误 406 Not Acceptable

标签 elasticsearch elasticsearch-java-api

我正在使用 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/

相关文章:

elasticsearch - Elasticsearch:如何找出一个值是否匹配列表中的任何值?

amazon-web-services - AWS Elastic Search下降大小突然

java - ElasticSearch Java API 查询生成器

spring-boot - 正确的SearchQuery的几个参数-Elasticsearch和Spring Boot

elasticsearch - 如何使用 ElasticSearch Java API 从 JSON 构建聚合

elasticsearch - 在单个请求的同一列内进行 Elasticsearch 多个正则表达式搜索?

java - Elasticsearch - 默认情况下不映射字段

elasticsearch - Elasticsearch中的多值或数组索引

elasticsearch - Elasticsearch:仅包含一个词排除查询

elasticsearch - ElasticSearch Java API与ElasticsearchTemplate