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

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

 更新時(shí)間:2024年02月29日 10:02:25   作者:A塵埃  
Elasticsearch是一個(gè)開源的分布式搜索和分析引擎,它被設(shè)計(jì)用于處理大規(guī)模數(shù)據(jù)集,它提供了一個(gè)分布式多用戶能力的全文搜索引擎,本文將給大家介紹SpringBoot集成ElasticSearch(ES)實(shí)現(xiàn)全文搜索功能,需要的朋友可以參考下

Document可以看作表中的一條記錄,Index可以看作Database數(shù)據(jù)庫,而Type就是里面的Table表。

一、依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>

二、在application.properties文件中添加ElasticSearch連接信息

server.port=8080
#es 配置信息
spring.main.allow-bean-definition-overriding=true
elasticsearch.host=127.0.0.1
elasticsearch.port=9300
elasticsearch.clustername=elasticsearch
elasticsearch.search.pool.size=5

啟動類

@SpringBootApplication
public class SSD08Application {

	public static void main(String[] args) {
		SpringApplication.run(SSD08Application.class, args);
	}

}

三、創(chuàng)建Elasticsearch配置類

@Slf4j
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.yoodb.study.demo08.service")
public class ElasticsearchConfig{
	
	@Value("${elasticsearch.host}")
	private String esHost;

	@Value("${elasticsearch.port}")
    private int esPort;

    @Value("${elasticsearch.clustername}")
    private String esClusterName;

    @Value("${elasticsearch.search.pool.size}")
    private Integer threadPoolSearchSize;

	@Bean
	public Client client() throws Exception{
		Settings esSettings = Settings.builder()
			.put("cluster.name",esClusterName)
			.put("client.transoprt.sniff",true)
			.put("thread_pool.search.size",threadPoolSearchSize)
			.build();
		return new PreBuiltTransportClient(esSettings)
			.addTransoprtAddress(new TransportAddress(InetAddress.getByName(esHost), esPort));
	}

	@Bean(name="elasticsearchTemplate")
	public ElasticsearchOperations elasticsearchTemplateCustom() throws Exception {
		
		ElasticsearchTemplate elasticsearchTemplate;
		try{
			elasticsearchTemplate = new ElasticsearchTemplate(client());
            return elasticsearchTemplate;
		}catch(Exception e){
			return new ElasticsearchTemplate(client());
		}
	}
}

四、創(chuàng)建Article實(shí)體類

@Document 作用在類,標(biāo)記實(shí)體類為文檔對象,一般有兩個(gè)屬性

indexName:對應(yīng)索引庫名稱

type:對應(yīng)在索引庫中的類型

shards:分片數(shù)量,默認(rèn)分5片

replicas:副本數(shù)量,默認(rèn)1份

  • @Id 作用在成員變量,標(biāo)記一個(gè)字段作為id主鍵
  • @Field 作用在成員變量,標(biāo)記為文檔的字段,并指定字段映射屬性

type:字段類型,取值是枚舉:FieldType

index:是否索引,布爾類型,默認(rèn)是true

store:是否存儲,布爾類型,默認(rèn)是false

analyzer和searchAnalyzer中參數(shù)名稱保持一直,ik_max_word可以改成ik_smart

ik_max_word和ik_smart的區(qū)別?

  • ik_max_word參數(shù)采用窮盡式的分詞,比如“我愛家鄉(xiāng)”,可能會分出“我”,“我愛”,“家鄉(xiāng)”等
  • ik_smart參數(shù)分的會比較粗,如上語句可能會分出“我愛”,“家鄉(xiāng)”這樣

如果想要搜索出的結(jié)果盡可能全,可以使用ik_max_word參數(shù),如果需要結(jié)果盡可能精確,可以使用ik_smart參數(shù)

Document(indexName = "blog",type = "article")
public class Article {
    /**
     * 主鍵ID
     */
    @Field(type = FieldType.Keyword)
    private String id;

    /**
     * 文章標(biāo)題
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String title;

    /**
     * 文章內(nèi)容
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String content;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

}

五、繼承ElasticsearchRepository接口

public interface ElasticRepository extends ElasticsearchRepository<Article, String> {

}

六、IElasticService接口以及實(shí)現(xiàn)類

public interface IElasticService {

    public void save(Article article);

    public void saveAll(List<Article> list);

    public Iterator<Article> findAll();

    public List<Article> findArticleByTitle(String title);

}
@Service("elasticService")
public class ElasticServiceImpl implements IElasticService {

    @Autowired
    private ElasticRepository elasticRepository;


    @Override
    public void save(Article article) {
        elasticRepository.deleteAll();
        elasticRepository.save(article);
    }

    @Override
    public void saveAll(List<Article> list) {
        elasticRepository.saveAll(list);
    }

    @Override
    public Iterator<Article> findAll() {
        return elasticRepository.findAll().iterator();
    }

    public List<Article> findArticleByTitle(String title) {
        //matchQuery 對關(guān)鍵字分詞后進(jìn)行搜索
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", title);
        QueryBuilders.commonTermsQuery("title",title);
        Iterable<Article> search = elasticRepository.search(matchQueryBuilder);
        Iterator<Article> iterator = search.iterator();
        List<Article> list = new ArrayList<>();
        while (iterator.hasNext()){
            Article next = iterator.next();
            list.add(next);
        }
        return list;
    }
}

七、控制層Controller

@Log4j2
@RestController
@RequestMapping("/elastic")
public class ElasticController {

    @Autowired
    private IElasticService elasticService;

    @GetMapping("/init")
    public void init(){
        String title = "關(guān)注“Java精選”微信公眾號";
        String content = "Java精選專注程序員推送一些Java開發(fā)知識,包括基礎(chǔ)知識、各大流行框架" +
                "(Mybatis、Spring、Spring Boot等)、大數(shù)據(jù)技術(shù)(Storm、Hadoop、MapReduce、Spark等)、" +
                "數(shù)據(jù)庫(Mysql、Oracle、NoSQL等)、算法與數(shù)據(jù)結(jié)構(gòu)、面試專題、面試技巧經(jīng)驗(yàn)、職業(yè)規(guī)劃以及" +
                "優(yōu)質(zhì)開源項(xiàng)目等。其中一部分由小編總結(jié)整理,另一部分來源于網(wǎng)絡(luò)上優(yōu)質(zhì)資源,希望對大家的學(xué)習(xí)" +
                "和工作有所幫助。";
        Article article = createArticle(title, content);
        elasticService.save(article);

        title = "關(guān)注素文宅博客";
        content = "素文宅博客主要關(guān)于一些Java技術(shù)類型文章分享。";
        article = createArticle(title, content);
        List<Article> list = new ArrayList<>();
        list.add(article);
        elasticService.saveAll(list);
    }

    public static Article createArticle(String title,String content){
        UUID uuid = UUID.randomUUID();
        String id = uuid.toString();
        Article article = new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        return article;
    }

    @GetMapping("/all")
    public Iterator<Article> all(){
        return elasticService.findAll();
    }

    @GetMapping("/key")
    public List<Article> all(String title){
        return elasticService.findArticleByTitle(title);
    }

}

運(yùn)行程序

1)啟動ElasticSearch服務(wù)

在這里插入圖片描述

在這里插入圖片描述

2)啟動SpringBoot項(xiàng)目訪問地址

初始化生成索引文件訪問地址

http://localhost:8080/elastic/init

獲取所有索引內(nèi)容訪問地址

http://localhost:8080/elastic/all

[{"id":"62e73653-4658-4d72-9b2e-e541a3ae7d53","title":"關(guān)注“Java精選”微信公眾號","content":"Java精選專注程序員推送一些Java開發(fā)知識,包括基礎(chǔ)知識、各大流行框架(Mybatis、Spring、Spring Boot等)、大數(shù)據(jù)技術(shù)(Storm、Hadoop、MapReduce、Spark等)、數(shù)據(jù)庫(Mysql、Oracle、NoSQL等)、算法與數(shù)據(jù)結(jié)構(gòu)、面試專題、面試技巧經(jīng)驗(yàn)、職業(yè)規(guī)劃以及優(yōu)質(zhì)開源項(xiàng)目等。其中一部分由小編總結(jié)整理,另一部分來源于網(wǎng)絡(luò)上優(yōu)質(zhì)資源,希望對大家的學(xué)習(xí)和工作有所幫助。"},{"id":"1e7c2f0b-e07e-4037-a3a3-3385d8b7d2c1","title":"關(guān)注素文宅博客","content":"素文宅博客主要關(guān)于一些Java技術(shù)類型文章分享。"}]

根據(jù)搜索條件,獲取索引內(nèi)容訪問地址

http://localhost:8080/elastic/key?title=Java%E7%B2%BE%E9%80%89

返回結(jié)果:

[{"id":"62e73653-4658-4d72-9b2e-e541a3ae7d53","title":"關(guān)注“Java精選”微信公眾號","content":"Java精選專注程序員推送一些Java開發(fā)知識,包括基礎(chǔ)知識、各大流行框架(Mybatis、Spring、Spring Boot等)、大數(shù)據(jù)技術(shù)(Storm、Hadoop、MapReduce、Spark等)、數(shù)據(jù)庫(Mysql、Oracle、NoSQL等)、算法與數(shù)據(jù)結(jié)構(gòu)、面試專題、面試技巧經(jīng)驗(yàn)、職業(yè)規(guī)劃以及優(yōu)質(zhì)開源項(xiàng)目等。其中一部分由小編總結(jié)整理,另一部分來源于網(wǎng)絡(luò)上優(yōu)質(zhì)資源,希望對大家的學(xué)習(xí)和工作有所幫助。"}]

以上就是SpringBoot集成ElasticSearch(ES)實(shí)現(xiàn)全文搜索功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot ElasticSearch全文搜索的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringMVC Restful api接口實(shí)現(xiàn)的代碼

    SpringMVC Restful api接口實(shí)現(xiàn)的代碼

    本篇文章主要介紹了SpringMVC Restful api接口實(shí)現(xiàn)的代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java字符串格式化,{}占位符根據(jù)名字替換實(shí)例

    Java字符串格式化,{}占位符根據(jù)名字替換實(shí)例

    這篇文章主要介紹了Java字符串格式化,{}占位符根據(jù)名字替換實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java輸出數(shù)組的3種方法

    Java輸出數(shù)組的3種方法

    這篇文章主要給大家介紹了關(guān)于Java輸出數(shù)組的3種方法,對于初學(xué)者來說,數(shù)組的輸入輸出是一個(gè)麻煩的問題,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    本文我們深入探索了Java中的Synchronized關(guān)鍵字,包括其互斥性和可重入性的特性,文章詳細(xì)介紹了Synchronized的三種使用方式:修飾代碼塊、修飾普通方法和修飾靜態(tài)方法,感興趣的朋友一起看看吧
    2025-04-04
  • Java線程的6種狀態(tài)及轉(zhuǎn)化方式

    Java線程的6種狀態(tài)及轉(zhuǎn)化方式

    本文詳細(xì)介紹了Java線程的六種狀態(tài)以及狀態(tài)之間的轉(zhuǎn)換關(guān)系,線程狀態(tài)包括NEW(新建)、RUNNABLE(運(yùn)行)、BLOCKED(阻塞)、WAITING(等待)、TIMED_WAITING(超時(shí)等待)和TERMINATED(終止)
    2024-09-09
  • 使用@DS輕松解決動態(tài)數(shù)據(jù)源的問題

    使用@DS輕松解決動態(tài)數(shù)據(jù)源的問題

    這篇文章主要介紹了使用@DS輕松解決動態(tài)數(shù)據(jù)源的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java用freemarker導(dǎo)出word實(shí)用示例

    Java用freemarker導(dǎo)出word實(shí)用示例

    本篇文章主要介紹了Java用freemarker導(dǎo)出word實(shí)用示例,使用freemarker的模板來實(shí)現(xiàn)功能,有需要的可以了解一下。
    2016-11-11
  • java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解

    java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解

    這篇文章主要介紹了java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解,簡單介紹了隊(duì)列結(jié)構(gòu)以應(yīng)用場景,涉及詳細(xì)實(shí)現(xiàn)代碼,還是比較不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-11-11
  • 使用mybatis-plus中Page進(jìn)行分頁不生效解決過程

    使用mybatis-plus中Page進(jìn)行分頁不生效解決過程

    在使用MyBatis-Plus的Page進(jìn)行分頁時(shí),如果發(fā)現(xiàn)分頁不生效,可能是由于未正確配置分頁插件,確保在配置類中正確引入了分頁插件,并且數(shù)據(jù)庫類型設(shè)置正確,同時(shí),檢查MybatisPlusConfig類是否被正確注入
    2025-12-12
  • MyBatis Generator配置生成接口和XML映射文件的實(shí)現(xiàn)

    MyBatis Generator配置生成接口和XML映射文件的實(shí)現(xiàn)

    本文介紹了配置MBG以生成Mapper接口和XML映射文件,過合理使用MBG和自定義生成策略,可以有效解決生成的Example類可能帶來的問題,使代碼更加簡潔和易于維護(hù)
    2025-02-02

最新評論

赤峰市| 巴林右旗| 南部县| 镇巴县| 遂昌县| 盈江县| 新绛县| 昌乐县| 廉江市| 日土县| 保靖县| 剑川县| 西盟| 永清县| 扬州市| 新密市| 荔浦县| 通海县| 镇远县| 安多县| 察雅县| 瑞安市| 平湖市| 富源县| 扬中市| 武胜县| 天等县| 枝江市| 嘉善县| 翁牛特旗| 永登县| 子长县| 霍林郭勒市| 独山县| 郸城县| 常山县| 浑源县| 枣强县| 张家港市| 七台河市| 甘孜|