Elasticsearch java RestHighLevelClient "Unable to parse response body" IllegalArgumentException: Required [index]
Asked Answered
S

3

18

I'm dealing with a problem when creating an index using the java RestHighLevelClient in Elasticsearch and my CreateIndexResponse object is in consequence null.

I am actually able to create the index, which I can confirm later querying it, but when I create the index, I get this exception. Here my code:

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

Elasticsearch returns the message of success with:

`HTTP 200 Success

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

And I am actually able to retrieve the index later with a GET call, but when the RestHighLevelClient tries to parse the response, using the following internal call:

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

The following exception happens:

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]

So basically what this is saying is that the following response cannot be parsed, but for me it looks pretty parsable:

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

Why does it tell me that the index is missing? Is it that I'm using wrongly the java client? This is the version:

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

Thanks in advance for the help!

Skutchan answered 17/2, 2018 at 14:27 Comment(2)
I had the same error when I used older verson of Elasticsearch than the client was. That was a compatibility problem, see the solution for that: discuss.elastic.co/t/… What ES version do you use?Overmeasure
stackoverflow.com/users/2864658/anime answered with the best fix. Must resolve this one.Guillermoguilloche
M
11

You need to either update your version dependency or add compatibility headers (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/) as for my case even latest version of Spring-data-elastic search doesn't support version 8+ of Elastic Search. Had to configure my client like this:

@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")};
 }

}

Molding answered 12/4, 2022 at 13:18 Comment(1)
I am getting error in writing the custom header function, can u please let me know the library used for that?Mccarter
L
0

There are two things that needs to be considered here. Below example consists of code use to connect to Elasticsearch 8.5 cluster.

  1. Addition of custom header

     @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")
         };
     }
    
  2. Validating dependency module

  • If spring-boot-starter-data-elasticsearch is used as a dependency, please check the valid version you need using https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch

  • In my case, because I'm using Elasticsearch version 8.5, I had to use the below in my POM. You can check the Compile Dependencies portion in the above webpage to check which version you need to be using.

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
          <version>3.0.5</version>
      </dependency>
    
Litharge answered 6/4, 2023 at 11:9 Comment(0)
S
0

I met the same issue using the opensearch, for me - I upgrade the jar dependency version :https://opensearch.org/docs/latest/clients/java/. then the issue is gone.

Stoltz answered 1/5, 2023 at 0:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.