Can I use elasticsearch-java with elasticsearch 7.5.2?
Asked Answered
A

4

1

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.
Almuce answered 17/8, 2022 at 7:55 Comment(0)
O
1

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.

Ohmage answered 17/8, 2022 at 9:23 Comment(2)
Thanks! I'll still use high level client instead.Almuce
Sure. if it helps you then accept answer .Ohmage
L
1

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);
Lissa answered 7/12, 2022 at 11:13 Comment(0)
E
0

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.

Estival answered 3/11, 2022 at 13:56 Comment(0)
B
0

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

}
Bergman answered 24/6, 2023 at 18:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.