SpringBoot集成ElasticSearch實現搜索功能
引言
在現代Web應用中,搜索功能是一個非常重要的特性。ElasticSearch是一個分布式的搜索和分析引擎,能夠快速地存儲、搜索和分析大量數據。Spring Data ElasticSearch提供了與ElasticSearch集成的簡便方式。本文將介紹如何在Spring Boot中集成ElasticSearch,實現基本的搜索功能。
什么是ElasticSearch
ElasticSearch是一個基于Lucene的開源搜索引擎,支持全文搜索、結構化搜索和分析,并能夠處理海量數據。它提供了一個分布式多租戶能力的全文搜索引擎,具有高度的可擴展性和實時性。
添加依賴
在Spring Boot項目中添加ElasticSearch的依賴。在pom.xml
文件中添加以下依賴項:
<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)建實體類
創(chuàng)建一個名為Article.java
的實體類:
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)建一個名為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)建服務層
創(chuàng)建一個名為ArticleService.java
的服務類:
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)建一個名為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); } }
運行ElasticSearch
確保ElasticSearch已經在本地運行。如果還沒有安裝ElasticSearch,可以從ElasticSearch官網下載并安裝。
測試搜索功能
啟動Spring Boot應用后,可以通過以下API進行測試:
保存文章:POST /articles
,請求體示例:
{ "title": "Spring Boot with ElasticSearch", "content": "Integrating ElasticSearch with Spring Boot..." }
搜索文章:GET /articles?title=Spring
,你將會得到包含“Spring”關鍵字的文章列表。
結論
通過本文的學習,你已經掌握了如何在Spring Boot中集成ElasticSearch,并實現基本的搜索功能。
到此這篇關于Spring Boot 集成ElasticSearch實現搜索功能的文章就介紹到這了,更多相關Spring Boot ElasticSearch搜索內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SchedulingConfigurer實現動態(tài)定時,導致ApplicationRunner無效解決
這篇文章主要介紹了SchedulingConfigurer實現動態(tài)定時,導致ApplicationRunner無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05