最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot結(jié)合ElasticSearch實(shí)現(xiàn)模糊查詢(xún)的項(xiàng)目實(shí)踐

 更新時(shí)間:2024年03月13日 11:23:18   作者:擁抱AI  
本文主要介紹了SpringBoot結(jié)合ElasticSearch實(shí)現(xiàn)模糊查詢(xún)的項(xiàng)目實(shí)踐,主要實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮功能,具有一定的參考價(jià)值,感興趣的可以了解一下

本文將詳細(xì)介紹如何使用SpringBoot整合ElasticSearch,實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮功能。我們將深入探討ElasticSearch的相關(guān)概念和技術(shù)細(xì)節(jié),以及如何使用SpringData Elasticsearch庫(kù)簡(jiǎn)化開(kāi)發(fā)過(guò)程。

1. 引言

ElasticSearch是一個(gè)基于Lucene構(gòu)建的開(kāi)源搜索引擎,它提供了一個(gè)分布式、多租戶(hù)的全文搜索引擎,具有高可靠性、可擴(kuò)展性和易用性。SpringBoot是Spring框架的一個(gè)模塊,它簡(jiǎn)化了基于Spring的應(yīng)用程序的開(kāi)發(fā)和部署。將SpringBoot與ElasticSearch整合,可以實(shí)現(xiàn)強(qiáng)大的搜索功能,如模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮等。

2. 環(huán)境準(zhǔn)備

在開(kāi)始之前,請(qǐng)確保已安裝Java和Maven,并配置好相應(yīng)的環(huán)境變量。接下來(lái),我們將創(chuàng)建一個(gè)SpringBoot項(xiàng)目,并添加ElasticSearch依賴(lài)。

2.1 創(chuàng)建SpringBoot項(xiàng)目

使用Spring Initializr(https://start.spring.io/)創(chuàng)建一個(gè)SpringBoot項(xiàng)目,選擇相應(yīng)的依賴(lài),如Spring Web、Spring Data Elasticsearch等。

2.2 添加ElasticSearch依賴(lài)

在項(xiàng)目的pom.xml文件中添加ElasticSearch依賴(lài):

<dependencies>
    <!-- SpringBoot Elasticsearch 依賴(lài) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <!-- 其他依賴(lài) -->
</dependencies>

3. 配置ElasticSearch

在application.properties或application.yml文件中配置ElasticSearch相關(guān)屬性:

# application.properties
spring.data.elasticsearch.cluster-name=my-cluster-name
spring.data.elasticsearch.cluster-nodes=localhost:9300

4. 創(chuàng)建ElasticSearch實(shí)體類(lèi)

創(chuàng)建一個(gè)ElasticSearch實(shí)體類(lèi),用于映射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)建一個(gè)繼承ElasticsearchRepository接口的repository接口,用于操作ElasticSearch索引:

public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
}

6. 實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)、高亮

6.1 模糊查詢(xún)

在ArticleRepository接口中添加一個(gè)自定義方法,用于實(shí)現(xiàn)模糊查詢(xún):

List<Article> findByTitleContaining(String title);

在Controller中添加一個(gè)接口,用于接收前端查詢(xún)參數(shù)并調(diào)用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中添加一個(gè)接口,用于接收前端提交的批量操作請(qǐng)求:

@PostMapping("/batch")
public ResponseEntity<String> batch(@RequestBody List<Article> articles) {
    for (Article article : articles) {
        articleRepository.save(article);
    }
    return ResponseEntity.ok("批量操作成功!");
}

6.3 排序

在ElasticsearchRepository接口中,預(yù)定義了許多排序的方法,如findByTitleOrderByCreateTimeDesc。如果這些方法無(wú)法滿(mǎn)足需求,可以使用@Query注解自定義查詢(xún),并指定排序規(guī)則:

@Query("{\"bool\": {\"must\": {\"match\": {\"title\": \"?0\"}}}}")
List<Article> findByTitle(String title, Sort sort);

6.4 分頁(yè)

在ElasticsearchRepository接口中,預(yù)定義了許多分頁(yè)的方法,如findByTitle(String title, Pageable pageable)。在Controller中,可以使用Pageable對(duì)象接收分頁(yè)參數(shù):

@GetMapping("/search")
public Page<Article> search(@RequestParam("title") String title, Pageable pageable) {
    return articleRepository.findByTitle(title, pageable);
}

6.5 高亮

在ElasticsearchRepository接口中,預(yù)定義了許多高亮的方法,如findByTitle(String title, Pageable pageable)。在Controller中,可以使用Pageable對(duì)象接收分頁(yè)參數(shù),并返回一個(gè)帶有高亮信息的Page對(duì)象:

@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. 完整代碼示例

將上述所有代碼片段組合在一起,我們得到了一個(gè)完整的SpringBoot+ElasticSearch實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮的示例。下面是完整的代碼示例:

// 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. 總結(jié)

本文詳細(xì)介紹了如何使用SpringBoot整合ElasticSearch,實(shí)現(xiàn)模糊查詢(xún)、批量CRUD、排序、分頁(yè)和高亮功能。請(qǐng)注意,實(shí)際部署時(shí),您可能需要根據(jù)實(shí)際情況調(diào)整ElasticSearch集群配置,以及索引的創(chuàng)建和映射策略。此外,對(duì)于生產(chǎn)環(huán)境,您可能還需要考慮更多的錯(cuò)誤處理和資源管理策略,例如處理可能出現(xiàn)的異常情況,以及優(yōu)化查詢(xún)性能等。

到此這篇關(guān)于SpringBoot結(jié)合ElasticSearch實(shí)現(xiàn)模糊查詢(xún)的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot ElasticSearch模糊查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

武川县| 京山县| 嘉峪关市| 古田县| 三亚市| 肃南| 涿州市| 浦东新区| 宿迁市| 交城县| 阳原县| 左云县| 斗六市| 赤壁市| 五常市| 双流县| 巴林右旗| 普格县| 珲春市| 昌平区| 佛学| 舞钢市| 玛曲县| 南部县| 宜兰县| 呼伦贝尔市| 满城县| 平利县| 余庆县| 岚皋县| 宝丰县| 临夏市| 维西| 和静县| 凭祥市| 班玛县| 普安县| 肥东县| 客服| 广东省| 通辽市|