java - 将Spring Boot与Elastic搜索集成的最佳方法

标签 java spring spring-boot maven elasticsearch

我是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/

相关文章:

java - 处理 3 离屏丑陋

java - 如何在 Java 中读取 "join"哈希表?

spring-boot - WebClient 第一次请求缓慢的解决方法

spring-boot - 扩展在kubernetes集群中运行的spring boot服务的推荐方法是什么?

java - 我缺少一个变量吗?

java - 在 Spring MVC 、 Hibernate 、 Oracle 项目中实现规则引擎所需的设计指南

java - 如何更好地处理@RequestParam(required=true)

java - Camel : define jms queue name from properites file

java - 为我的项目创建类似 Slf4j 的注释

java - 来自 Java 的存储过程调用 - FUNCTION 不存在?