【问题标题】:Best way to integrate Spring boot with the Elastic search将 Spring Boot 与 Elastic 搜索集成的最佳方式
【发布时间】:2020-10-01 20:38:29
【问题描述】:

我是弹性搜索的新手。我们正在使用 Elastic search 构建一个 Spring Boot 应用程序。

目前,我们必须使用 Spring Boot 2.1.3.RELEASE 但我们可以使用最新的稳定 Elastic 搜索版本。

做了一些研发,发现以下两个依赖项可以与 Elasticsearch 集成。

1. elasticsearch-rest-high-level-client
2. spring-boot-starter-data-elasticsearch

可能还有其他方法可以将 Spring Boot 与 Elastic 搜索集成。

谁能帮助确定将 Spring Boot 与 Elastic 搜索集成的最佳方式?

按照上面提供的 Spring boot 版本,我应该使用哪个版本的 Elastic search?

【问题讨论】:

  • 我有一个使用 Spring boot 2.1.0.RELEASE 和 Elasticsearch 6.2.2 构建的应用程序。确保使用相同的版本。大多数导入是 pom.xml 和版本。这对我来说很有用。 github.com/Georges73/Touring-fullstack/tree/master/…
  • 感谢您的建议!我不能使用最新版本的 Elastic 搜索吗?如果不是那为什么?
  • 由于 data-elasticsearch 仅支持 ES transportclient,它不适用于 rest-high-level-client(请参阅我的第一个示例)为了性能,我会选择 elasticsearch-rest-high-level-client 作为在我的第二个示例中显示。
  • 您也可以将两者结合起来(请参阅下面的答案)。结合使用时,您将使用 spring-data-elasticsearch,因此您可以使用其酷(但有限)的内置查询语言。对于更多复杂查询,请使用 rest-high-level-client。我所说的复杂是指分析等。
  • 我一定要用Spring boot!

标签: java spring spring-boot maven elasticsearch


【解决方案1】:

来到 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);
  }

以上两个操作在弹性索引中创建一个文档(行),并根据ID从弹性索引中删除一个文档(行)。

更多参考请见:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *
*根据您的需要更改版本

您可以参考this 以获得更多帮助

【讨论】:

  • 感谢您的回答,我发现最好只使用“高级休息客户端”。如果我使用高级休息客户端进行集成,我可以将“elasticsearch-7.7.1”与“Spring Boot 2.1.3.RELEASE”一起使用吗?
猜你喜欢
  • 1970-01-01
  • 2010-10-24
  • 2020-09-30
  • 2011-09-12
  • 2010-10-08
  • 1970-01-01
  • 2021-09-11
  • 2010-10-12
  • 2010-11-26
相关资源
最近更新 更多