问题
I'm new to Elastic search. We are building a Spring boot application with Elastic search.
Currently, we are bound to use Spring Boot 2.1.3.RELEASE but we can use the latest stable Elastic search version.
Done some R&D and found below two dependencies to integrate with Elastic search.
1. elasticsearch-rest-high-level-client
2. spring-boot-starter-data-elasticsearch
There might be other ways of integrating the Spring boot with Elastic search.
Could anyone help with identifying the best way to integrating the Spring boot with Elastic search?
Which version of Elastic search should I use as per the above-provided Spring boot version?
回答1:
Coming to the Elasticsearch Version, Refer this site:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
For using Elasticsearch with SpringBoot, we include three dependencies:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
As You can see My Elasticsearch Version is 6.2.2(to match the server side version) and my Spring Version is 2.1.13.RELEASE.
There are basically 2 Clients used. I would suggest you to use Rest High Level Client. The other one is Transport Client.
Here is a how you can integrate Rest High Level Client to your application:
@Configuration
public class ElasticClientService extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200").build();
return RestClients.create(clientConfiguration).rest();
}
}
Once the client is created, only thing left is to perform CRUD operations.
@Autowired
ElasticClientService client;
public void save(Object object, String id, String type, String indexName) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> objectMap = objectMapper.convertValue(object, Map.class);
IndexRequest indexRequest = new IndexRequest(indexName, type, id);
indexRequest.source(objectMap);
IndexResponse response = client.elasticsearchClient().index(indexRequest);
}
public void deleteById(String id, String type, String indexName) throws IOException {
DeleteRequest request = new DeleteRequest(indexName, type, id);
DeleteResponse deleteResponse = client.elasticsearchClient().delete(request);
}
The above two operations create a Document(row) in elastic index and delete a document(row) from elastic index according to ID.
For more reference See :https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *
*Change Version According to your need
You could Refer this for further assistance
来源:https://stackoverflow.com/questions/62337314/best-way-to-integrate-spring-boot-with-the-elastic-search