SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能
引言
在現(xiàn)代Web應(yīng)用中,搜索功能是一個(gè)非常重要的特性。ElasticSearch是一個(gè)分布式的搜索和分析引擎,能夠快速地存儲(chǔ)、搜索和分析大量數(shù)據(jù)。Spring Data ElasticSearch提供了與ElasticSearch集成的簡(jiǎn)便方式。本文將介紹如何在Spring Boot中集成ElasticSearch,實(shí)現(xiàn)基本的搜索功能。
什么是ElasticSearch
ElasticSearch是一個(gè)基于Lucene的開源搜索引擎,支持全文搜索、結(jié)構(gòu)化搜索和分析,并能夠處理海量數(shù)據(jù)。它提供了一個(gè)分布式多租戶能力的全文搜索引擎,具有高度的可擴(kuò)展性和實(shí)時(shí)性。
添加依賴
在Spring Boot項(xiàng)目中添加ElasticSearch的依賴。在pom.xml
文件中添加以下依賴項(xiàng):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
配置ElasticSearch
在application.properties
文件中配置ElasticSearch連接信息:
spring.elasticsearch.rest.uris=http://localhost:9200
創(chuàng)建實(shí)體類
創(chuàng)建一個(gè)名為Article.java
的實(shí)體類:
package com.example.demo; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "articles") public class Article { @Id private String id; private String title; private String content; // Getters and setters }
創(chuàng)建Repository接口
創(chuàng)建一個(gè)名為ArticleRepository.java
的接口,繼承ElasticsearchRepository
:
package com.example.demo; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface ArticleRepository extends ElasticsearchRepository<Article, String> { List<Article> findByTitleContaining(String title); }
創(chuàng)建服務(wù)層
創(chuàng)建一個(gè)名為ArticleService.java
的服務(wù)類:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ArticleService { @Autowired private ArticleRepository articleRepository; public Article saveArticle(Article article) { return articleRepository.save(article); } public List<Article> findArticlesByTitle(String title) { return articleRepository.findByTitleContaining(title); } }
創(chuàng)建控制層
創(chuàng)建一個(gè)名為ArticleController.java
的控制器類:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/articles") public class ArticleController { @Autowired private ArticleService articleService; @PostMapping public Article saveArticle(@RequestBody Article article) { return articleService.saveArticle(article); } @GetMapping public List<Article> findArticles(@RequestParam String title) { return articleService.findArticlesByTitle(title); } }
運(yùn)行ElasticSearch
確保ElasticSearch已經(jīng)在本地運(yùn)行。如果還沒有安裝ElasticSearch,可以從ElasticSearch官網(wǎng)下載并安裝。
測(cè)試搜索功能
啟動(dòng)Spring Boot應(yīng)用后,可以通過以下API進(jìn)行測(cè)試:
保存文章:POST /articles
,請(qǐng)求體示例:
{ "title": "Spring Boot with ElasticSearch", "content": "Integrating ElasticSearch with Spring Boot..." }
搜索文章:GET /articles?title=Spring
,你將會(huì)得到包含“Spring”關(guān)鍵字的文章列表。
結(jié)論
通過本文的學(xué)習(xí),你已經(jīng)掌握了如何在Spring Boot中集成ElasticSearch,并實(shí)現(xiàn)基本的搜索功能。
到此這篇關(guān)于Spring Boot 集成ElasticSearch實(shí)現(xiàn)搜索功能的文章就介紹到這了,更多相關(guān)Spring Boot ElasticSearch搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎
這篇文章主要介紹了Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring請(qǐng)求路徑帶參數(shù)URL使用注解的寫法說明
這篇文章主要介紹了Spring請(qǐng)求路徑帶參數(shù)URL使用注解的寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí),導(dǎo)致ApplicationRunner無效解決
這篇文章主要介紹了SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí),導(dǎo)致ApplicationRunner無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05SpringBoot+React實(shí)現(xiàn)計(jì)算個(gè)人所得稅
本文將以個(gè)人所得稅的計(jì)算為例,使用React+SpringBoot+GcExcel來實(shí)現(xiàn)這一功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-09-09如何基于回調(diào)實(shí)現(xiàn)Java的異步調(diào)用
這篇文章主要介紹了如何基于回調(diào)實(shí)現(xiàn)Java的異步調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06