我是Elastic Search的新手。我们正在使用Elastic搜索构建Spring Boot应用程序。
当前,我们必须使用Spring Boot 2.1.3.RELEASE,但是我们可以使用最新的稳定Elastic搜索版本。
完成了一些研发工作,发现下面两个要与Elastic search集成的依赖项。
1. elasticsearch-rest-high-level-client
2. spring-boot-starter-data-elasticsearch
可能还有其他方法可以将Spring Boot与Elastic search集成在一起。
谁能帮助您确定将Spring Boot与Elastic search集成的最佳方法?
根据上面提供的Spring引导版本,我应该使用哪个版本的Elastic search?
最佳答案
进入Elasticsearch版本,请访问以下站点:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
为了在SpringBoot中使用Elasticsearch,我们包括三个依赖项:
<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(以匹配服务器端版本),而我的 Spring 版本为2.1.13.RELEASE。
基本上有2个客户端。我建议您使用Rest High Level Client。另一个是传输客户端。
这是将Rest High Level Client集成到应用程序中的方法:
@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操作。
@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);
}
上面的两个操作会创建一个具有 flex 索引的Document(行),并根据ID从 flex 索引中删除一个文档(行)。
有关更多引用,请参见:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *
*根据您的需要更改版本
您可以引用this获得更多帮助
关于java - 将Spring Boot与Elastic搜索集成的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62337314/