欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring?boot?3使用?elasticsearch?提供搜索建議的實(shí)例詳解

 更新時(shí)間:2023年08月29日 17:02:53   作者:北漂的菜小白  
這篇文章主要介紹了spring?boot3使用elasticsearch提供搜索建議,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

業(yè)務(wù)場(chǎng)景

用戶輸入內(nèi)容,快速返回建議,示例效果如下

技術(shù)選型

  • spring boot 3
  • elasticsearch server 7.17.4
  • spring data elasticsearch 5.0.1
  • elasticsearch-java-api 8.5.3

pom.xml

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  </dependency>
  <dependency>
     <groupId>org.elasticsearch</groupId>
     <artifactId>elasticsearch</artifactId>
     <version>8.5.3</version>
 </dependency>

yml

spring:
  elasticsearch:
    uris: http://127.0.0.1:9200
  data:
    elasticsearch:
      repositories:
        enabled: true

實(shí)體類

為了啟動(dòng)時(shí)候自己創(chuàng)建相關(guān)的index,以及存儲(chǔ)搜索內(nèi)容

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.CompletionField;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.suggest.Completion;
/**
 * 業(yè)務(wù)搜索建議
 * @author chunyang.leng
 * @date 2023-08-21 14:24
 */
@Document(indexName = "biz_suggest")
public class BizSuggestDocument {
    @Id
    private Long id;
    /**
     * 標(biāo)題,可以用于糾錯(cuò),不參與搜索建議
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String name;
    /**
     * 自動(dòng)補(bǔ)全標(biāo)題,搜索建議使用的對(duì)象
     */
    @CompletionField(analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private Completion completionName;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Completion getCompletionName() {
        return completionName;
    }
    public void setCompletionName(Completion completionName) {
        this.completionName = completionName;
    }
}

搜索結(jié)果對(duì)象

/**
 * 搜索建議返回對(duì)象
 * @author chunyang.leng
 * @date 2023-08-21 19:02
 */
public class SuggestVO {
    /**
     * 數(shù)據(jù)id
     */
    private Long id;
    /**
     * 內(nèi)容
     */
    private String text;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}

搜索業(yè)務(wù)層

數(shù)據(jù)導(dǎo)入時(shí)候,因?yàn)橛袛?shù)據(jù)格式要求,必須使用實(shí)體類進(jìn)行寫(xiě)入

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.CompletionSuggester;
import co.elastic.clients.elasticsearch.core.search.Suggester;
import co.elastic.clients.elasticsearch.core.search.Suggestion;
import co.elastic.clients.elasticsearch.core.search.TermSuggester;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * @author chunyang.leng
 * @date 2023-08-18 18:29
 */
@Component
public class SuggestionServiceImpl implements SuggestionService {
    /**
     * 搜索建議 key
     */
    private static final String SUGGEST_TAG = "suggest_query";
    /**
     * 糾錯(cuò)key
     */
    private static final String TERM_TAG = "suggest_team";
    @Autowired
    private ElasticsearchClient elasticsearchClient;
    /**
     * 根據(jù) 關(guān)鍵詞,返回搜索建議
     *
     * @param match 搜索關(guān)鍵詞
     * @return 搜索建議,10條
     */
    @Override
    public List<SuggestVO> suggest(String match) throws IOException {
        SearchRequest completionSuggestSearchRequest = new SearchRequest
            .Builder()
            .suggest(
                new Suggester
                    .Builder()
                    .suggesters(SUGGEST_TAG, builder -> builder.prefix(match)
                        .completion(new CompletionSuggester
                            .Builder()
                            .field("completionName")
                            .size(10)
                            .build()
                        )
                    )
                    .build())
            .build();
        SearchResponse<BizSuggestDocument> completionSuggestSearch = elasticsearchClient.search(completionSuggestSearchRequest, BizSuggestDocument.class);
        Map<String, List<Suggestion<BizSuggestDocument>>> suggest = completionSuggestSearch.suggest();
        List<Suggestion<BizSuggestDocument>> suggestions = suggest.get(SUGGEST_TAG);
        return suggestions
            .parallelStream()
            .flatMap(x -> x.completion()
                .options()
                .stream()
                .map(o -> {
                      // 原始數(shù)據(jù)對(duì)象,如果有需要,可以對(duì)其進(jìn)行操作
                    BizSuggestDocument source = o.source();
                    String text = o.text();
                    String idValue = o.id();
                    Long id = Long.valueOf(idValue);
                    SuggestVO vo = new SuggestVO();
                    vo.setId(id);
                    vo.setText(text);
                    return vo;
                }))
            .collect(Collectors.toList());
    }
}

導(dǎo)入數(shù)據(jù)

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.suggest.Completion;
/**
 * @author chunyang.leng
 * @date 2023-08-21 18:09
 */
@SpringBootTest
public class EsTest {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Test
    public void test() {
        BizSuggestDocument document = new BizSuggestDocument();
        document.setId(1L);
        document.setName("飛翔的世界");
        String[] s = "你的世界1.0,我的世界2.0".split(",");
        Completion completion = new Completion(s);
        completion.setWeight(10);
        document.setCompletionName(completion);
        elasticsearchTemplate.save(document);
        BizSuggestDocument document2 = new BizSuggestDocument();
        document2.setId(2L);
        document2.setName("路人甲乙丙");
        String[] s2 = "你的滑板鞋1.0,我的滑板鞋2.0".split(",");
        Completion completion1 = new Completion(s2);
        completion1.setWeight(5);
        document2.setCompletionName(completion1);
        elasticsearchTemplate.save(document2);
    }
}

POSTMAN 測(cè)試結(jié)果如下

在這里插入圖片描述

在這里插入圖片描述

到此這篇關(guān)于spring boot 3使用 elasticsearch 提供搜索建議的文章就介紹到這了,更多相關(guān)spring boot elasticsearch搜索建議內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • struts2自定義MVC框架

    struts2自定義MVC框架

    這篇文章主要為大家詳細(xì)介紹了struts2如何自定義MVC框架,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Java中關(guān)于OAuth2.0的原理分析

    Java中關(guān)于OAuth2.0的原理分析

    這篇文章主要介紹了Java中關(guān)于OAuth2.0的原理分析,OAuth是一個(gè)關(guān)于授權(quán)的開(kāi)放網(wǎng)絡(luò)標(biāo)準(zhǔn),允許用戶授權(quán)第三 方應(yīng)用訪問(wèn)他們存儲(chǔ)在另外的服務(wù)提供者上的信息,而不需要將用戶名和密碼提供給第三方移動(dòng)應(yīng)用或分享他們數(shù)據(jù)的所有內(nèi)容,需要的朋友可以參考下
    2023-09-09
  • Java利用poi讀取Excel詳解實(shí)現(xiàn)

    Java利用poi讀取Excel詳解實(shí)現(xiàn)

    Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫(xiě)的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫(xiě),意為簡(jiǎn)潔版的模糊實(shí)現(xiàn)
    2022-07-07
  • Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板

    Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 基于Flyway實(shí)現(xiàn)簡(jiǎn)化Spring Boot項(xiàng)目部署

    基于Flyway實(shí)現(xiàn)簡(jiǎn)化Spring Boot項(xiàng)目部署

    這篇文章主要介紹了基于Flyway實(shí)現(xiàn)簡(jiǎn)化Spring Boot項(xiàng)目部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼

    SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 淺析對(duì)java枚舉類型的認(rèn)識(shí)

    淺析對(duì)java枚舉類型的認(rèn)識(shí)

    在本文里我們給大家分享了關(guān)于對(duì)java枚舉類型的認(rèn)識(shí)和相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。
    2019-03-03
  • Java經(jīng)典排序算法之快速排序代碼實(shí)例

    Java經(jīng)典排序算法之快速排序代碼實(shí)例

    這篇文章主要介紹了Java經(jīng)典排序算法之快速排序代碼實(shí)例,快速排序?qū)崿F(xiàn)的思想是指通過(guò)一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨(dú)立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對(duì)這兩部分?jǐn)?shù)據(jù)分別進(jìn)行快速排序,需要的朋友可以參考下
    2023-10-10
  • SpringBoot+RabbitMQ實(shí)現(xiàn)消息可靠傳輸詳解

    SpringBoot+RabbitMQ實(shí)現(xiàn)消息可靠傳輸詳解

    消息的可靠傳輸是面試必問(wèn)的問(wèn)題之一,保證消息的可靠傳輸主要在生產(chǎn)端開(kāi)啟?comfirm?模式,RabbitMQ?開(kāi)啟持久化,消費(fèi)端關(guān)閉自動(dòng)?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實(shí)現(xiàn)消息可靠傳輸,需要的可以參考一下
    2022-05-05
  • 基于Java將Excel科學(xué)計(jì)數(shù)法解析成數(shù)字

    基于Java將Excel科學(xué)計(jì)數(shù)法解析成數(shù)字

    這篇文章主要介紹了基于Java將Excel科學(xué)計(jì)數(shù)法解析成數(shù)字,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論