Reputation: 23
I hava tried many version of elasticsearch-java but none of it can work.
So, my elasticsearch version is 7.5.2, and the error resonse is like this:
org.elasticsearch.client.ResponseException: method [POST], host [***:80], URI [/location/_search?typed_keys=true], status line [HTTP/1.1 406 Not Acceptable]
{"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=7] is not supported","status":406}
When I trying to set the default header like this:
setDefaultHeaders(new Header[]{
new BasicHeader("Content-type", "application/json")
})
And the error response changed to this:
co.elastic.clients.transport.TransportException: [es/search] Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch instance, and that any networking filters are preserving that header.
Upvotes: 1
Views: 3352
Reputation: 1039
If someone uses spring data approach (maybe to have repository abstraction and other features) with ClientConfiguration and had the same problem (in my case I was connecting elasticsearch-java 8.6.2 to elasticsearch 7.13.1 and got a org.springframework.dao.DataAccessResourceFailureException), maybe will find this useful:
package com.myapplication.configuration;
import java.util.List;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients.ElasticsearchClientConfigurationCallback;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import co.elastic.clients.transport.TransportUtils;
import lombok.Setter;
@Setter
@Configuration
public class ElasticSearchClientConfig extends ElasticsearchConfiguration {
@Value("${elastic.host}")
private String host;
@Value("${elastic.port}")
private String port;
@Value("${elastic.fingerprint}")
private String fingerprint;
@Value("${elastic.user}")
private String user;
@Value("${elastic.password}")
private String password;
@Override
public ClientConfiguration clientConfiguration() {
return ClientConfiguration.builder()
.connectedTo(String.format("%s:%s", host, port))
.usingSsl(TransportUtils.sslContextFromCaFingerprint(fingerprint))
.withBasicAuth(user, password)
.withClientConfigurer(
ElasticsearchClientConfigurationCallback.from(
httpClientBuilder -> httpClientBuilder
.disableAuthCaching()
.setDefaultHeaders(
List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
.addInterceptorLast(
(HttpResponseInterceptor) (response, context) -> response.addHeader("X-Elastic-Product", "Elasticsearch"))
)
)
.build();
}
}
Upvotes: 0
Reputation: 1336
Adding to @puppylpg answer, I tested below code and worked fine.
SSLContext sslContext = TransportUtils
.sslContextFromCaFingerprint(fingerprint);
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
credsProv.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(login, password)
);
// Create the low-level esClient
RestClient restClient = RestClient
.builder(new HttpHost(esHost, esPort, protocol))
.setDefaultHeaders(new Header[]{
new BasicHeader("Content-type", "application/json")
})
.setHttpClientConfigCallback(hc -> hc
.setSSLContext(sslContext)
.setDefaultCredentialsProvider(credsProv)
.addInterceptorLast( (HttpResponseInterceptor)
(response, context) ->
response.addHeader("X-Elastic-Product", "Elasticsearch"))
).build();
// Create the esTransport with a Jackson mapper
ElasticsearchTransport esTransport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
// And create the API esClient
ElasticsearchClient esClient = new ElasticsearchClient(esTransport);
Upvotes: 1
Reputation: 1220
You can still use elasticsearch-java
client if you prefer it than high level rest client, which is deprecated already.
All you have to do is to add default X-Elastic-Product: Elasticsearch
header for all the responses by low version elasticsearch server like this.
As for the Content-type: application/json
request header, it may be optional(eg: no need to add this header for requests to 7.12.x elasticsearch). However, in your situation(7.5.2), this header should exist.
Note: keep in mind that the elasticsearch-java client may not be totally compatible with low version elasticsearch server even if the response header check is passed.
Upvotes: 0
Reputation: 5486
The Elastic Java API Client is forward-compatible and not backward. So you can not use it with Elastic 7.5 version. You can use it only with the 7.16 or greater version.
Elasticsearch Java high level client you can use with 7.5 version but it is deprecated now.
Upvotes: 1