• Elasticsearch和sboot整合


    Elasticsearch是一个开源的、高性能的分布式搜索引擎,可以实现全文搜索、分析和存储等功能。Spring Boot是一个开源的Java框架,可以用于快速开发和部署应用程序。

    将Elasticsearch和Spring Boot整合可以使得我们更加方便地使用Elasticsearch进行搜索和数据分析。以下是整合的步骤:

    1. 添加Elasticsearch的依赖

    在Spring Boot工程中,可以在pom.xml文件中添加以下依赖,其中elasticsearch和elasticsearch-rest-high-level-client是Elasticsearch相关的依赖:

    1. <dependency>
    2. <groupId>org.elasticsearch</groupId>
    3. <artifactId>elasticsearch</artifactId>
    4. <version>6.8.17</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.elasticsearch.client</groupId>
    8. <artifactId>elasticsearch-rest-high-level-client</artifactId>
    9. <version>6.8.17</version>
    10. </dependency>

    1. 配置Elasticsearch的连接

    在Spring Boot工程中,可以在application.properties或application.yml文件中添加以下配置,注意将host和port替换为Elasticsearch的实际地址和端口:

    spring.data.elasticsearch.cluster-nodes=host:port
    

    1. 创建Elasticsearch的客户端

    在Spring Boot的项目中,可以使用RestHighLevelClient创建Elasticsearch客户端。以下是一个简单的示例代码:

    1. @Configuration
    2. public class ElasticsearchClientConfig {
    3. @Value("${spring.data.elasticsearch.cluster-nodes}")
    4. private String clusterNodes;
    5. @Bean
    6. public RestHighLevelClient restHighLevelClient() {
    7. String[] nodes = clusterNodes.split(",");
    8. HttpHost[] httpHosts = new HttpHost[nodes.length];
    9. for (int i = 0; i < nodes.length; i++) {
    10. String node = nodes[i];
    11. httpHosts[i] = new HttpHost(node.split(":")[0], Integer.parseInt(node.split(":")[1]), "http");
    12. }
    13. return new RestHighLevelClient(RestClient.builder(httpHosts));
    14. }
    15. }

    1. 创建Elasticsearch的存储库

    在Spring Boot的项目中,可以使用ElasticsearchRepository来操作Elasticsearch中的数据。以下是一个示例代码:

    1. public interface BookRepository extends ElasticsearchRepository<Book, String> {
    2. List<Book> findByTitle(String title);
    3. List<Book> findByAuthor(String author);
    4. }

    其中,Book是用于存储书籍信息的实体类,可以根据需要进行定义。在该接口中,实现了根据标题和作者进行搜索的功能。

    1. 进行数据操作

    在Spring Boot的项目中,可以使用ElasticsearchRepository中定义的方法来进行数据操作。以下是一个示例代码:

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. @Autowired
    5. private BookRepository bookRepository;
    6. @PostMapping("/")
    7. public Book save(@RequestBody Book book) {
    8. return bookRepository.save(book);
    9. }
    10. @GetMapping("/{id}")
    11. public Book getById(@PathVariable("id") String id) {
    12. return bookRepository.findById(id).orElse(null);
    13. }
    14. @GetMapping("/")
    15. public List<Book> getByTitle(@RequestParam("title") String title) {
    16. return bookRepository.findByTitle(title);
    17. }
    18. }

    以上代码实现了通过RESTful API进行数据操作的功能,包括保存图书信息、根据ID查询图书信息、根据标题查询图书信息等。

  • 相关阅读:
    力扣题目训练(20)
    PHP生成word文档的简单实现
    瑞_23种设计模式_策略模式
    【深度学习】第三章:卷积神经网络
    数学小抄: 概率角度推导Kalman Filter
    功能测试常用的测试用例大全
    Struts2的拦截器
    酷晚报:6月24日Web3业界重点消息大汇总
    el-input-number输入框超过限制后自动变为最大值
    SAP PA MM 后台配置
  • 原文地址:https://blog.csdn.net/NanCheng_666/article/details/134405748