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 ElasticsearchOperations
from 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 ?