'Elasticsearch java RestHighLevelClient "Unable to parse response body" IllegalArgumentException: Required [index]

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!



Solution 1:[1]

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

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anime