• springboot 整合es


    Spring Boot可以轻松地与Elasticsearch进行整合,以实现高效的搜索和分析功能。

    以下是如何在Spring Boot应用程序中使用Elasticsearch的步骤:

    1.添加依赖项

    pom.xml文件中添加以下依赖项:

    
       org.springframework.boot
       spring-boot-starter-data-elasticsearch
    
    
    • 1
    • 2
    • 3
    • 4

    2.配置Elasticsearch

    在Spring Boot应用程序的配置文件application.properties中添加以下配置:

    spring.data.elasticsearch.cluster-name=elasticsearch
    spring.data.elasticsearch.cluster-nodes=localhost:9300
    
    • 1
    • 2

    这里假设您正在运行Elasticsearch节点,该节点位于本地主机上的端口9300。

    3.创建Elasticsearch存储库

    创建一个包含所有必需方法的Elasticsearch存储库接口。例如:

    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    
    public interface ProductRepository extends ElasticsearchRepository {
    }
    
    • 1
    • 2
    • 3
    • 4

    4.定义数据模型

    定义与Elasticsearch文档相对应的数据模型。例如,以下是一个名为“Product”的类:

    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    
    @Document(indexName = "products", type = "product")
    public class Product {
       @Id
       private String id;
       private String name;
       private String description;
       private double price;
       // getters and setters
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.使用存储库

    在Spring Boot应用程序的服务层中使用存储库进行搜索和保存数据。例如:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ProductService {
       @Autowired
       private ProductRepository productRepository;
    
       public Iterable searchByName(String name) {
          return productRepository.findByName(name);
       }
    
       public void save(Product product) {
          productRepository.save(product);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6.测试您的应用程序

    您现在可以启动您的Spring Boot应用程序并测试它是否可以与Elasticsearch集成。例如,您可以像以下方式搜索产品:

    @Autowired
    private ProductService productService;
    
    @GetMapping("/search")
    public List search(@RequestParam String name) {
       Iterable products = productService.searchByName(name);
       List productList = new ArrayList<>();
       products.forEach(productList::add);
       return productList;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    此外,您还可以使用Elasticsearch的REST API测试您的应用程序。例如,您可以通过以下方式创建一个名为“Product”的索引:

    PUT /products
    {
       "settings": {
          "number_of_shards": 1
       },
       "mappings": {
          "product": {
             "properties": {
                "name": {
                   "type": "text"
                },
                "description": {
                   "type": "text"
                },
                "price": {
                   "type": "double"
                }
             }
          }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    然后,您可以向“Product”索引添加文档:

    POST /products/product
    {
       "name": "iPhone X",
       "description": "Apple iPhone",
       "price": 999.99
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    中介者模式(Mediator Pattern)
    美团动态线程池实践思路,开源了
    Nginx优化
    系统和系统实例-软件方法(下)第9章分析类图案例篇Part07
    你的哪些骚操作会导致Segmentation Fault😂
    三肽Gly-Cys-Gly、88440-55-5
    Ribbon负载均衡
    ES Query DSL-复合查询和关联查询
    SkyWalking分布式链路追踪学习
    uni-app发布后iOS端页面背景图片上下滑动问题
  • 原文地址:https://blog.csdn.net/qq_36151389/article/details/132855290