本文將詳細介紹如何使用SpringBoot整合ElasticSearch,實現(xiàn)模糊查詢、批量CRUD、排序、分頁和高亮功能。我們將深入探討ElasticSearch的相關概念和技術細節(jié),以及如何使用SpringData Elasticsearch庫簡化開發(fā)過程。
1. 引言
ElasticSearch是一個基于Lucene構建的開源搜索引擎,它提供了一個分布式、多租戶的全文搜索引擎,具有高可靠性、可擴展性和易用性。SpringBoot是Spring框架的一個模塊,它簡化了基于Spring的應用程序的開發(fā)和部署。將SpringBoot與ElasticSearch整合,可以實現(xiàn)強大的搜索功能,如模糊查詢、批量CRUD、排序、分頁和高亮等。
2. 環(huán)境準備
在開始之前,請確保已安裝Java和Maven,并配置好相應的環(huán)境變量。接下來,我們將創(chuàng)建一個SpringBoot項目,并添加ElasticSearch依賴。
2.1 創(chuàng)建SpringBoot項目
使用Spring Initializr(https://start.spring.io/)創(chuàng)建一個SpringBoot項目,選擇相應的依賴,如Spring Web、Spring Data Elasticsearch等。
2.2 添加ElasticSearch依賴
在項目的pom.xml文件中添加ElasticSearch依賴:
<dependencies>
<!-- SpringBoot Elasticsearch 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 其他依賴 -->
</dependencies>
3. 配置ElasticSearch
在application.properties或application.yml文件中配置ElasticSearch相關屬性:
# application.properties
spring.data.elasticsearch.cluster-name=my-cluster-name
spring.data.elasticsearch.cluster-nodes=localhost:9300
4. 創(chuàng)建ElasticSearch實體類
創(chuàng)建一個ElasticSearch實體類,用于映射ElasticSearch索引中的文檔:
@Document(indexName = "blog", type = "article")
public class Article {
@Id
private String id;
private String title;
private String content;
// getter and setter
}
5. 創(chuàng)建ElasticSearch repository接口
創(chuàng)建一個繼承ElasticsearchRepository接口的repository接口,用于操作ElasticSearch索引:
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
}
6. 實現(xiàn)模糊查詢、批量CRUD、排序、分頁、高亮
6.1 模糊查詢
在ArticleRepository接口中添加一個自定義方法,用于實現(xiàn)模糊查詢:
List<Article> findByTitleContaining(String title);
在Controller中添加一個接口,用于接收前端查詢參數(shù)并調用repository方法:
@RestController
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
@GetMapping("/search")
public List<Article> search(@RequestParam("title") String title) {
return articleRepository.findByTitleContaining(title);
}
}
6.2 批量CRUD
在Controller中添加一個接口,用于接收前端提交的批量操作請求:
@PostMapping("/batch")
public ResponseEntity<String> batch(@RequestBody List<Article> articles) {
for (Article article : articles) {
articleRepository.save(article);
}
return ResponseEntity.ok("批量操作成功!");
}
6.3 排序
在ElasticsearchRepository接口中,預定義了許多排序的方法,如findByTitleOrderByCreateTimeDesc。如果這些方法無法滿足需求,可以使用@Query注解自定義查詢,并指定排序規(guī)則:
@Query("{\"bool\": {\"must\": {\"match\": {\"title\": \"?0\"}}}}")
List<Article> findByTitle(String title, Sort sort);
6.4 分頁
在ElasticsearchRepository接口中,預定義了許多分頁的方法,如findByTitle(String title, Pageable pageable)。在Controller中,可以使用Pageable對象接收分頁參數(shù):
@GetMapping("/search")
public Page<Article> search(@RequestParam("title") String title, Pageable pageable) {
return articleRepository.findByTitle(title, pageable);
}
6.5 高亮
在ElasticsearchRepository接口中,預定義了許多高亮的方法,如findByTitle(String title, Pageable pageable)。在Controller中,可以使用Pageable對象接收分頁參數(shù),并返回一個帶有高亮信息的Page對象:
@GetMapping("/search")
public Page<Article> search(@RequestParam("title") String title, Pageable pageable) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(matchQuery("title", title));
queryBuilder.withHighlightFields(new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"));
queryBuilder.withPageable(pageable);
NativeSearchQuery searchQuery = queryBuilder.build();
Page<Article> articles = articleRepository.search(searchQuery);
return articles;
}
7. 完整代碼示例
將上述所有代碼片段組合在一起,我們得到了一個完整的SpringBoot+ElasticSearch實現(xiàn)模糊查詢、批量CRUD、排序、分頁和高亮的示例。下面是完整的代碼示例:文章來源:http://www.zghlxwxcb.cn/news/detail-857814.html
// Article.java
@Document(indexName = "blog", type = "article")
public class Article {
@Id
private String id;
private String title;
private String content;
// getter and setter
}
// ArticleRepository.java
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
List<Article> findByTitleContaining(String title);
Page<Article> findByTitle(String title, Pageable pageable);
}
// ArticleController.java
@RestController
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
@GetMapping("/search")
public Page<Article> search(@RequestParam("title") String title, Pageable pageable) {
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withQuery(matchQuery("title", title));
queryBuilder.withHighlightFields(new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"));
queryBuilder.withPageable(pageable);
NativeSearchQuery searchQuery = queryBuilder.build();
Page<Article> articles = articleRepository.search(searchQuery);
return articles;
}
@PostMapping("/batch")
public ResponseEntity<String> batch(@RequestBody List<Article> articles) {
for (Article article : articles) {
articleRepository.save(article);
}
return ResponseEntity.ok("批量操作成功!");
}
}
// application.properties
spring.data.elasticsearch.cluster-name=my-cluster-name
spring.data.elasticsearch.cluster-nodes=localhost:9300
8. 總結
本文詳細介紹了如何使用SpringBoot整合ElasticSearch,實現(xiàn)模糊查詢、批量CRUD、排序、分頁和高亮功能。請注意,實際部署時,您可能需要根據(jù)實際情況調整ElasticSearch集群配置,以及索引的創(chuàng)建和映射策略。此外,對于生產環(huán)境,您可能還需要考慮更多的錯誤處理和資源管理策略,例如處理可能出現(xiàn)的異常情況,以及優(yōu)化查詢性能等。
最后,如果您對SpringBoot+ElasticSearch整合或其他相關主題有更多的問題,歡迎在評論區(qū)留言討論。文章來源地址http://www.zghlxwxcb.cn/news/detail-857814.html
到了這里,關于Java實戰(zhàn):SpringBoot+ElasticSearch 實現(xiàn)模糊查詢的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!