将Spring Boot与Elastic搜索集成的最佳方法 [英] Best way to integrate Spring boot with the Elastic search
问题描述
我是Elastic Search的新手.我们正在使用Elastic search构建一个Spring Boot应用程序.
I'm new to Elastic search. We are building a Spring boot application with Elastic search.
当前,我们必须使用Spring Boot 2.1.3 RELEASE,但是我们可以使用最新的稳定Elastic搜索版本.
Currently, we are bound to use Spring Boot 2.1.3.RELEASE but we can use the latest stable Elastic search version.
进行了一些研发,发现以下两个依赖项可以与Elastic search集成.
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
可能还有其他方法可以将Spring Boot与Elastic search集成在一起.
There might be other ways of integrating the Spring boot with Elastic search.
有人可以帮助确定将Spring Boot与Elastic search集成的最佳方法吗?
我应按照上面提供的Spring引导版本使用哪个版本的Elastic search?
推荐答案
来到Elasticsearch版本,请访问以下站点:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
Coming to the Elasticsearch Version, Refer this site:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
对于将Elasticsearch与SpringBoot结合使用,我们包括三个依赖项:
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>
如您所见,我的Elasticsearch版本为6.2.2(以匹配服务器端版本),而我的春季版本为2.1.13.RELEASE.
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.
基本上有2个客户端使用.我建议您使用Rest High Level Client.另一个是传输客户端.
There are basically 2 Clients used. I would suggest you to use Rest High Level Client. The other one is Transport Client.
以下是将Rest High Level 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();
}
}
一旦创建了客户端,剩下的就是执行CRUD操作.
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);
}
以上两个操作在弹性索引中创建一个Document(行),并根据ID从弹性索引中删除一个文档(行).
The above two operations create a Document(row) in elastic index and delete a document(row) from elastic index according to ID.
更多参考,请参见: https://www.elastic.co/guide/zh-CN/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *
*根据您的需要更改版本
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
这篇关于将Spring Boot与Elastic搜索集成的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!