亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能

 更新時(shí)間:2025年03月24日 09:13:58   作者:阿里渣渣java研發(fā)組-群主  
本文主要介紹了Spring Boot 集成ElasticSearch實(shí)現(xiàn)搜索功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

引言

在現(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)文章

最新評(píng)論