Home > Software design >  How to use stable RestHighLevelClient with Elasticsearch?
How to use stable RestHighLevelClient with Elasticsearch?

Time:10-17

I have searched for so many posts but I couldn't find a proper way to use Elastic Search with spring boot application because I am totally new to elastic search.

My only dependency is: org.springframework.boot spring-boot-starter-data-elasticsearch 2.7.3

My config class is:

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.backend.repository.elasticsearchrepository")
@ComponentScan(basePackages = {"com.backend.model.elasticsearchmodel"})
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {

    @Value("${spring.elasticsearch.url}")
    public String elasticsearchUrl;

    @Value("${spring.elasticsearch.username}")
    public String username;

    @Value("${spring.elasticsearch.password}")
    public String password;


    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration config = ClientConfiguration.builder()
                .connectedTo(elasticsearchUrl)
                .withBasicAuth(username, password)
                .build();

        return RestClients.create(config).rest();
    }
}

Here RestHighLevelClient is shown as deprecated. And my repository class is:

package com.backend.repository.elasticsearchrepository;

import com.backend.model.elasticsearchmodel.EsOffice;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.UUID;

public interface ESOfficeRepository extends ElasticsearchRepository<EsOffice, UUID> {
}

When I call the methods of this repository then it works fine but while storing the data it is returning error message even if it adds the data successfully.

2022-10-15 00:00:15.608 ERROR 51607 --- [nio-8080-exec-2] c.a.a.exception.GlobalExceptionHandler   : Unable to parse response body for Response{requestLine=POST /office/_doc?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}; nested exception is java.lang.RuntimeException: Unable to parse response body for Response{requestLine=POST /office/_doc?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}

Which POM dependency what kind of repository should I use and How can I configure it in my config file ? I need these 3 that compatible with each other ?

CodePudding user response:

Spring Data Elasticsearch 4.4 (which is pulled in by Spring Boot 2.7.3) is build with the Elasticsearch libraries in version 7.17, this is problematic when running against an Elasticsearch cluster in version 8. Youhave basically two options:

  1. Downgrade your cluster to version 7.17.6 (the latest 7.17 currently available) i f this is possible.

  2. You can try and see if setting the compatibility headers (see the Spring Data Elasticsearch documentation section 5.3.1 for more info). This should work, but I encountered cases where the response from the cluster still wasn't readable with a 7.17 client. - I had issues opened with Elasticsearch and they were resolved, but there still might be hidden traps.

  • Related