SpringBoot+Elasticsearch实现高效全文搜索

👉 这是一个或许对你有用的社群

🐱 一对一交流/面试小册/简历优化/求职解惑,欢迎加入「芋道快速开发平台」知识星球。下面是星球提供的部分资料: 

50017dfbe213f830424a8a88a2b1e70f.gif

👉这是一个或许对你有用的开源项目

国产 Star 破 10w+ 的开源项目,前端包括管理后台 + 微信小程序,后端支持单体和微服务架构。

功能涵盖 RBAC 权限、SaaS 多租户、数据权限、商城、支付、工作流、大屏报表、微信公众号等等功能:

  • Boot 仓库:https://gitee.com/zhijiantianya/ruoyi-vue-pro

  • Cloud 仓库:https://gitee.com/zhijiantianya/yudao-cloud

  • 视频教程:https://doc.iocoder.cn

【国内首批】支持 JDK 21 + SpringBoot 3.2.2、JDK 8 + Spring Boot 2.7.18 双版本 

来源:blog.csdn.net/qq_63519395
/article/details/136100825

bfd0813f3bed5ccd4edc3c59ac85a78a.jpeg


在现代应用程序中,对于大量数据的高效管理和快速检索是至关重要的。Elasticsearch(以下简称ES)作为一款开源的全文搜索引擎,为开发者提供了强大而灵活的搜索解决方案。

本文将介绍如何通过Spring Boot框架整合Elasticsearch,实现高效的全文搜索功能。

创建SpringBoot项目

首先,在你的开发环境中创建一个新的Spring Boot项目。你可以选择使用Spring Initializr(https://start.spring.io/)进行项目初始化,选择所需的依赖和项目设置。

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/ruoyi-vue-pro

  • 视频教程:https://doc.iocoder.cn/video/

添加Elasticsearch依赖

在项目的pom.xml文件中,添加Elasticsearch客户端库的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

这个依赖将引入Spring Data Elasticsearch,使得在Spring Boot应用中更容易地使用Elasticsearch。

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能

  • 项目地址:https://github.com/YunaiV/yudao-cloud

  • 视频教程:https://doc.iocoder.cn/video/

配置Elasticsearch连接

在application.properties文件中,配置Elasticsearch连接信息:

spring:
  data:
    elasticsearch:
      cluster-nodes: localhost:9200

确保你的Elasticsearch实例在本地运行,并监听在默认端口9200上。

创建实体类

定义一个简单的实体类,用于映射到Elasticsearch索引中的文档。例如,如果你要存储文档的标题和内容,可以创建如下类:

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "documents", type = "document")
public class DocumentEntity {

    @Id
    private String id;
    private String title;
    private String content;

    // 省略构造函数和getter/setter方法
}

创建Elasticsearch Repository

使用Spring Data Elasticsearch提供的ElasticsearchRepository接口,创建一个用于与Elasticsearch进行交互的Repository:

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface DocumentRepository extends ElasticsearchRepository<DocumentEntity, String> {
    
    // 可以添加自定义的查询方法
    
}

编写Service层

创建一个Service类,用于封装业务逻辑,调用Repository层进行数据操作:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocumentService {

    @Autowired
    private DocumentRepository documentRepository;

    public List<DocumentEntity> searchDocuments(String keyword) {
        // 可以根据业务需求调用Repository中的方法进行搜索
        return documentRepository.findByTitleOrContent(keyword, keyword);
    }

    public void saveDocument(DocumentEntity document) {
        documentRepository.save(document);
    }
}

创建Controller层

编写一个Controller类,处理来自前端或其他服务的HTTP请求,并调用Service层的方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/documents")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @GetMapping("/search")
    public List<DocumentEntity> searchDocuments(@RequestParam String keyword) {
        return documentService.searchDocuments(keyword);
    }

    @PostMapping("/add")
    public void addDocument(@RequestBody DocumentEntity document) {
        documentService.saveDocument(document);
    }
}

测试

启动你的Spring Boot应用程序,并使用Postman或其他工具测试搜索和添加文档的功能。

总结

通过这个简单的示例,你已经成功地将Elasticsearch集成到了Spring Boot应用程序中。这使得你能够轻松地实现全文搜索功能,提升了应用程序对大量数据的管理和检索效率。当然,根据具体业务需求,你还可以进一步优化和扩展这个基础架构,使用Elasticsearch提供的更高级功能。

希望这篇文章能够帮助你在Spring Boot项目中利用Elasticsearch实现强大的全文搜索功能。


欢迎加入我的知识星球,全面提升技术能力。

👉 加入方式,长按”或“扫描”下方二维码噢

ac749a95072d9c1c073cafc26e5f04ee.png

星球的内容包括:项目实战、面试招聘、源码解析、学习路线。

da114576b7b935376fc770bf93707eb8.png

ffd82fdae21c7f36f05c694d0bcac3af.pngb333a038c6e71644ef4189a73804b787.png9e9d7a42f0d640a8f7c570e05012b8b9.png829efb3c4b02fc3d5d068d5bfc2a2c8b.png

文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java框架,用于构建独立的、可扩展的、基于微服务的应用程序。它提供了一种快速、简单的方式来构建和部署应用程序,也简化了与各种数据库、消息队列和其他外部系统进行集成的过程。同时,Spring Boot提供了一套强大的开发工具和功能,方便开发人员进行开发、测试和部署。 Elasticsearch是一个基于Lucene的分布式搜索和分析引擎,它被广泛应用于构建实时搜索、数据分析和数据存储的解决方案。Elasticsearch具有高性能、可扩展、可靠和易于使用的特点,可以处理大规模的数据,并提供全文搜索、聚合分析和实时监控等功能。 Oracle是一种关系型数据库管理系统(RDBMS),它是全球领先的企业级数据库解决方案之一。Oracle提供了高度可靠和安全的数据管理功能,同时支持事务处理、并发性控制、数据恢复和备份等重要特性。它还提供了丰富的管理和开发工具,方便开发人员进行数据库的设计、开发和管理。 在使用Spring Boot开发应用程序时,可以通过集成Elasticsearch和Oracle来满足不同的需求。使用Elasticsearch,可以轻松地实现全文搜索、聚合分析和实时监控等功能。而Oracle数据库可以用于存储结构化数据,并提供事务处理、数据完整性和安全性等特性。通过使用这两个技术,可以构建出高效、可靠和安全的应用程序,满足不同场景下的各种需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值