springboot 整合es

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

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

1.添加依赖项

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

复制代码
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.配置Elasticsearch

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

复制代码
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300

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

3.创建Elasticsearch存储库

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

复制代码
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ProductRepository extends ElasticsearchRepository<Product, Long> {
}

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
}

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<Product> searchByName(String name) {
      return productRepository.findByName(name);
   }

   public void save(Product product) {
      productRepository.save(product);
   }
}

6.测试您的应用程序

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

复制代码
@Autowired
private ProductService productService;

@GetMapping("/search")
public List<Product> search(@RequestParam String name) {
   Iterable<Product> products = productService.searchByName(name);
   List<Product> productList = new ArrayList<>();
   products.forEach(productList::add);
   return productList;
}

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

复制代码
PUT /products
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "product": {
         "properties": {
            "name": {
               "type": "text"
            },
            "description": {
               "type": "text"
            },
            "price": {
               "type": "double"
            }
         }
      }
   }
}

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

复制代码
POST /products/product
{
   "name": "iPhone X",
   "description": "Apple iPhone",
   "price": 999.99
}
相关推荐
韩立学长1 小时前
基于Springboot泉州旅游攻略平台d5h5zz02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游
Dxy12393102161 小时前
Elasticsearch 索引与映射:为你的数据打造一个“智能仓库”
大数据·elasticsearch·搜索引擎
摇滚侠1 小时前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
打工的小王3 小时前
Spring Boot(三)Spring Boot整合SpringMVC
java·spring boot·后端
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 高校体育场馆管理系统为例,包含答辩的问题和答案
java·spring boot
vx_Biye_Design3 小时前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
翱翔-蓝天3 小时前
为什么“看起来很规范”的后端项目反而臃肿且性能下降
spring boot
80530单词突击赢4 小时前
JavaWeb进阶:SpringBoot核心与Bean管理
java·spring boot·后端
long3165 小时前
Aho-Corasick 模式搜索算法
java·数据结构·spring boot·后端·算法·排序算法
独断万古他化5 小时前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密