教程:Spring Boot中集成Elasticsearch的步骤

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 教程:Spring Boot中集成Elasticsearch的步骤

教程:Spring Boot中集成Elasticsearch的步骤


在当今大数据时代,搜索功能对于许多应用程序至关重要。Elasticsearch作为一款开源的分布式搜索引擎,提供了强大的全文搜索和分析能力,广泛应用于日志分析、实时数据分析和搜索引擎等场景。本文将详细介绍如何在Spring Boot应用中集成Elasticsearch,为开发者展示一条通向高效搜索解决方案的道路。


准备工作

在开始之前,请确保你已经完成以下准备工作:

  • JDK 8及以上版本
  • Maven作为项目构建工具
  • Spring Boot框架
  • Elasticsearch服务器

确保你的开发环境已经配置好,并且可以访问到Elasticsearch服务器。

集成Spring Boot与Elasticsearch

添加依赖

首先,在你的Spring Boot项目的pom.xml文件中添加以下依赖:

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

这个依赖将会自动配置Elasticsearch的相关组件,使得我们可以方便地在Spring Boot应用中使用Elasticsearch。

配置Elasticsearch连接

application.propertiesapplication.yml中添加Elasticsearch的连接配置:

spring.data.elasticsearch.cluster-nodes=localhost:9200
spring.data.elasticsearch.cluster-name=my-elasticsearch-cluster

这里,cluster-nodes指定了Elasticsearch服务器的地址和端口,cluster-name是Elasticsearch集群的名称。

定义实体类

接下来,定义一个实体类,并使用Spring Data的注解来映射到Elasticsearch中的索引和文档类型:

package cn.juwatech.example;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "my_index", type = "my_type")
public class Book {
    @Id
    private String id;
    private String title;
    private String author;
    // Getters and setters
    // Constructors
    // Other fields and methods
}

在这个例子中,我们定义了一个简单的Book类,使用了@Document注解来指定索引名称和文档类型,@Id注解表示文档的唯一标识。

编写Repository接口

接下来,创建一个继承自ElasticsearchRepository的接口来操作Elasticsearch中的数据:

package cn.juwatech.example;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface BookRepository extends ElasticsearchRepository<Book, String> {
    List<Book> findByTitle(String title);
    List<Book> findByAuthor(String author);
}

通过继承ElasticsearchRepository接口,我们可以方便地进行索引数据的增删改查操作。

使用示例

添加数据

现在,让我们来看一个简单的示例,如何向Elasticsearch中添加数据:

package cn.juwatech.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BookService {
    @Autowired
    private BookRepository bookRepository;
    public void addBook() {
        Book book = new Book();
        book.setId("1");
        book.setTitle("Spring Boot in Action");
        book.setAuthor("Craig Walls");
        bookRepository.save(book);
    }
}

在这个例子中,我们通过BookService将一本书保存到Elasticsearch中。

查询数据

接下来,我们来查询Elasticsearch中的数据:

package cn.juwatech.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class BookSearchService {
    @Autowired
    private BookRepository bookRepository;
    public List<Book> searchByTitle(String title) {
        return bookRepository.findByTitle(title);
    }
    public List<Book> searchByAuthor(String author) {
        return bookRepository.findByAuthor(author);
    }
}

这里,我们创建了一个BookSearchService来进行按标题和作者的搜索操作。

总结

通过本文的教程,我们学习了如何在Spring Boot应用中集成Elasticsearch,并通过实例代码展示了基本的数据操作和搜索功能。从配置依赖、连接Elasticsearch,到定义实体类和操作Repository,我们覆盖了整个集成和使用过程。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
Java Maven
2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决
这篇文章是一份关于Maven的安装和配置指南,包括下载、环境变量设置、配置文件修改、IDEA集成Maven以及解决jar包下载问题的方法。
2022最新版超详细的Maven下载配置教程、IDEA中集成maven(包含图解过程)、以及导入项目时jar包下载不成功的问题解决
|
1月前
|
网络协议 Java API
SpringBoot整合Elasticsearch-Rest-Client、测试保存、复杂检索
这篇文章介绍了如何在SpringBoot中整合Elasticsearch-Rest-Client,并提供了保存数据和进行复杂检索的测试示例。
SpringBoot整合Elasticsearch-Rest-Client、测试保存、复杂检索
|
1月前
|
前端开发 Java Maven
【前端学java】全网最详细的maven安装与IDEA集成教程!
【8月更文挑战第12天】全网最详细的maven安装与IDEA集成教程!
66 2
【前端学java】全网最详细的maven安装与IDEA集成教程!
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14613 24
|
1月前
|
人工智能 Java API
JeecgBoot 低代码平台快速集成 Spring AI
Spring 通过 Spring AI 项目正式启用了 AI(人工智能)生成提示功能。本文将带你了解如何在 Jeecg Boot 应用中集成生成式 AI,以及 Spring AI 如何与模型互动,包含 RAG 功能。
93 3
|
27天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
28天前
|
缓存 NoSQL Java
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
|
2月前
|
JSON 缓存 Java
Spring Boot集成 Swagger2 展现在线接口文档
本节课详细分析了 Swagger 的优点,以及 Spring Boot 如何集成 Swagger2,包括配置,相关注解的讲解,涉及到了实体类和接口类,以及如何使用。最后通过页面测试,体验了 Swagger 的强大之处,基本上是每个项目组中必备的工具之一,所以要掌握该工具的使用,也不难。
|
1月前
|
自然语言处理 Java 关系型数据库
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码【完结篇】
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码【完结篇】
26 0
|
1月前
|
自然语言处理 Java 索引
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
28 0