-1

I'm creating new Java POC in order to test Eelasticsearch 8.4 features.

To do this, i found a configuration file on internet :

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.poc.elastic")
@ComponentScan(basePackages = {"com.poc"})
public class ElasticConfig {

    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration
                = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();

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

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}

I also create a repository class :

public interface ItemRepository extends ElasticsearchRepository<Item, String> {}

Now, i just want to create a quality code and no longer use RestHighLevelClient because it is deprecated.

I have read there is a new implementation but i don't know how to create ElasticsearchOperationsfrom new ElasticsearchClient (@EnableElasticsearchRepositories uses elasticsearchTemplate by default).

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.poc.elastic")
@ComponentScan(basePackages = {"com.poc"})
public class ElasticConfig {
    @Bean
    public RestClient getRestClient() {
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost:9200", 9200)).build();
        return restClient;
    }

    @Bean
    public ElasticsearchTransport getElasticsearchTransport() {
        return new RestClientTransport(
                getRestClient(), new JacksonJsonpMapper());
    }


    @Bean
    public ElasticsearchClient getElasticsearchClient() {
        ElasticsearchClient client = new ElasticsearchClient(getElasticsearchTransport());
        return client;
    }
}

I'm using spring 2.7.3.

Any idea ?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Jérémy
  • 391
  • 2
  • 7
  • 20

1 Answers1

-1

The new Elasticsearch client is available as an optional way to connect to Elasticsearch in version 4.4, that is what Spring Boot 2.7.3 uses. Check the Spring Data Elasticsearch documentation for how to integrate this new client.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66