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
}
相关推荐
需要82613 小时前
Spring Boot 启动流程:run() 里到底发生了什么
java·spring boot·后端
Elastic 中国社区官方博客13 小时前
Elastic 线下 Meetup 将于 2026 年 12 月 19 号下午在西安举行
大数据·数据库·elasticsearch·搜索引擎·ai·数据分析·全文检索
wangfenglei12345613 小时前
jenkins权限配置
运维·jenkins
SelectDB技术团队14 小时前
存算分离建表与 Storage Vault 配置全流程:日志集群从建库到弹性扩缩容的命令链
elasticsearch·日志·数据湖·apache doris·日志数据·数据库选型·多模湖仓
凤山老林15 小时前
Spring Boot 集成 Spring Modulith:搭一个能拆微服务的模块化单体
spring boot·spring·微服务
今年下半年15 小时前
【Spring Boot】集成 Elasticsearch:中文分词与关键字高亮对接文档
spring boot·elasticsearch·中文分词
jonyleek15 小时前
JVS私有化文档系统运维避坑指南:5大断点检测与加固实践(K8S+MinIO+ES)
elasticsearch·minio·clamav·jvs·k8s运维·文档系统·权限同步
骑着蜗牛撵大象32715 小时前
CentOS 7 单机拉起 Elasticsearch 7.17.29:从内核报错到公网可达的全链路记录
elasticsearch·centos·jenkins·restful·内核调优·公网部署
m0_5873830015 小时前
GogoAI自助健身门店解决方案核心技术实现与落地部署全流程实操指南
运维·spring boot·信息可视化·架构·需求分析
打工仔折腾 AI16 小时前
CentOS 7 单机部署 Elasticsearch 7.17.29:启动报错排查与 CRUD 验证
人工智能·后端·python·elasticsearch·性能优化·centos·jenkins