Reputation: 367
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!
Upvotes: 18
Views: 28435
Reputation: 603
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.
Upvotes: 0
Reputation: 11
There are two things that needs to be considered here. Below example consists of code use to connect to Elasticsearch 8.5 cluster.
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")
};
}
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>
Upvotes: 0
Reputation: 111
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")};
}
}
Upvotes: 11