Elasticsearch java RestHighLevelClient“Unable to parse response body”IllegalArgumentException:需要[index]

yiytaume  于 2023-04-11  发布在  ElasticSearch
关注(0)|答案(2)|浏览(565)

我在Elasticsearch中使用javaRestHighLevelClient创建索引时遇到问题,我的CreateIndexResponse对象在consequence中为null。
我实际上能够创建索引,我可以稍后查询它来确认,但是当我创建索引时,我得到了这个异常。下面是我的代码:

`CreateIndexRequest request = new CreateIndexRequest("myindex"); 
CreateIndexResponse createIndexResponse = client.indices().create(request);`

Elasticsearch返回成功的消息:

`HTTP 200 Success

{
  "acknowledged": true,
  "shards_acknowledged": true
}`

实际上,我可以稍后通过GET调用检索索引,但当RestHighLevelClient尝试解析响应时,使用以下内部调用:

//Type of the response converter: CheckedFunction<Req, Request, IOException>    requestConverter
responseConverter.apply(response);

发生以下异常:

java.io.IOException: Unable to parse response body for 
Response{requestLine=PUT /myindex?master_timeout=30s&timeout=30s HTTP/1.1, 
host=http://localhost:9200, response=HTTP/1.1 200 OK}
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:507)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:474)
at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:77)
at hello.client.HelloClient.createSynch(HelloClient.java:84)
at hello.main.Main.main(Main.java:25)
Caused by: java.lang.IllegalArgumentException: Required [index]

所以基本上这就是说,下面的响应不能被解析,但对我来说,它看起来很容易解析:

Response{requestLine=PUT /myindex?master_timeout=30s&timeout=30s HTTP/1.1, 
host=http://localhost:9200, response=HTTP/1.1 200 OK}

为什么它告诉我索引丢失了?是我错误地使用了java客户端吗?这是版本:

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.2.1</version>
    </dependency>
</dependencies>`

提前感谢您的帮助!

3j86kqsm

3j86kqsm1#

你需要更新你的版本依赖或添加兼容性头(https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/),就我的情况而言,即使是最新版本的Spring-data-elastic search也不支持版本8+的Elastic Search。不得不这样配置我的客户端:

@Configuration
@EnableElasticsearchRepositories(basePackages = "*")
public class ElasticsearchClientConfig {

  @Value("${elasticsearch.host}")
  private String host;

  @Value("${elasticsearch.port}")
  private int port;

  @Value("${elasticsearch.protocol}")
  private String protocol;

  @Value("${elasticsearch.username}")
  private String userName;

  @Value("${elasticsearch.password}")
  private String password;

  @Bean(destroyMethod = "close")
  public RestHighLevelClient restClient() {

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

    RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocol))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
            .setDefaultHeaders(compatibilityHeaders());

    return new RestHighLevelClient(builder);
  }

  private Header[] compatibilityHeaders() {
    return new Header[]{new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"), new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")};
 }

}

vlurs2pr

vlurs2pr2#

这里需要考虑两件事。下面的示例由连接到Elasticsearch 8.5集群的代码组成。
1.添加自定义页眉

@Bean(name = "client", destroyMethod = "close")
 public RestHighLevelClient client() {

     String uri = applicationConfig.getElasticSearchDB();
     String[] hosts = uri.split(Constants.COMMA);
     List<HttpHost> httpHosts = new ArrayList<>();
     Arrays.stream(hosts).forEach(a -> httpHosts.add(new HttpHost(String.valueOf(a.split(Constants.COLON)[0]), Integer.parseInt(a.split(Constants.COLON)[1]), "http")));

     HttpHost[] esHosts = new HttpHost[httpHosts.size()];
     httpHosts.toArray(esHosts);

     return new RestHighLevelClient(RestClient
             .builder(esHosts)
             .setDefaultHeaders(compatibilityHeaders()));
 }

 private Header[] compatibilityHeaders() {
     return new Header[]{
             new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"),
             new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")
     };
 }

1.正在验证依赖模块

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
      <version>3.0.5</version>
  </dependency>

相关问题