简体   繁体   English

将 Spring 引导与弹性搜索集成的最佳方式

[英]Best way to integrate Spring boot with the Elastic search

I'm new to Elastic search.我是弹性搜索的新手。 We are building a Spring boot application with Elastic search.我们正在使用弹性搜索构建 Spring 引导应用程序。

Currently, we are bound to use Spring Boot 2.1.3.RELEASE but we can use the latest stable Elastic search version.目前,我们必须使用 Spring Boot 2.1.3.RELEASE 但我们可以使用最新的稳定 Elastic 搜索版本。

Done some R&D and found below two dependencies to integrate with Elastic search.做了一些研发,发现下面两个依赖项可以与 Elasticsearch 集成。

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.可能还有其他方法可以将 Spring 引导与弹性搜索集成。

Could anyone help with identifying the best way to integrating the Spring boot with Elastic search?任何人都可以帮助确定将 Spring 引导与弹性搜索集成的最佳方法吗?

Which version of Elastic search should I use as per the above-provided Spring boot version?根据上面提供的 Spring 引导版本,我应该使用哪个版本的 Elasticsearch?

Coming to the Elasticsearch Version, Refer this site:来到 Elasticsearch 版本,请参阅此站点:
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions

For using Elasticsearch with SpringBoot, we include three dependencies:为了将 Elasticsearch 与 SpringBoot 一起使用,我们包括三个依赖项:

        <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.如您所见,我的 Elasticsearch 版本是 6.2.2(与服务器端版本匹配),我的 Spring 版本是 2.1.13.RELEASE。

There are basically 2 Clients used.基本上使用了2个客户端。 I would suggest you to use Rest High Level Client.我建议您使用 Rest 高级客户端。 The other one is Transport Client.另一个是传输客户端。

Here is a how you can integrate Rest High Level Client to your application:以下是将 Rest 高级客户端集成到您的应用程序的方法:

@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.创建客户端后,剩下的就是执行 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);
  }

The above two operations create a Document(row) in elastic index and delete a document(row) from elastic index according to ID.以上两个操作在弹性索引中创建一个 Document(row),并根据 ID 从弹性索引中删除一个 document(row)。

For more reference See: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-document-delete.html *如需更多参考,请参阅: 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你可以参考这个以获得进一步的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM