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

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

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

業(yè)務場景

用戶輸入內容,快速返回建議,示例效果如下

技術選型

  • 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

實體類

為了啟動時候自己創(chuàng)建相關的index,以及存儲搜索內容

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è)務搜索建議
 * @author chunyang.leng
 * @date 2023-08-21 14:24
 */
@Document(indexName = "biz_suggest")
public class BizSuggestDocument {
    @Id
    private Long id;
    /**
     * 標題,可以用于糾錯,不參與搜索建議
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String name;
    /**
     * 自動補全標題,搜索建議使用的對象
     */
    @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;
    }
}

搜索結果對象

/**
 * 搜索建議返回對象
 * @author chunyang.leng
 * @date 2023-08-21 19:02
 */
public class SuggestVO {
    /**
     * 數(shù)據(jù)id
     */
    private Long id;
    /**
     * 內容
     */
    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è)務層

數(shù)據(jù)導入時候,因為有數(shù)據(jù)格式要求,必須使用實體類進行寫入

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";
    /**
     * 糾錯key
     */
    private static final String TERM_TAG = "suggest_team";
    @Autowired
    private ElasticsearchClient elasticsearchClient;
    /**
     * 根據(jù) 關鍵詞,返回搜索建議
     *
     * @param match 搜索關鍵詞
     * @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ù)對象,如果有需要,可以對其進行操作
                    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());
    }
}

導入數(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 測試結果如下

在這里插入圖片描述

在這里插入圖片描述

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

相關文章

  • Java中return的用法(兩種)

    Java中return的用法(兩種)

    這篇文章主要介紹了Java中return的用法(兩種)的相關資料,需要的朋友可以參考下
    2016-01-01
  • Java實現(xiàn)FIFO、LRU、LFU、OPT頁面置換算法

    Java實現(xiàn)FIFO、LRU、LFU、OPT頁面置換算法

    本文主要介紹了Java實現(xiàn)FIFO、LRU、LFU、OPT頁面置換算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Java完整實現(xiàn)記事本代碼

    Java完整實現(xiàn)記事本代碼

    這篇文章主要介紹了Java實現(xiàn)的簡易記事本,較為詳細的分析了基于java實現(xiàn)記事本程序的完整過程,具有一定參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Spring Data MongoDB構建高效數(shù)據(jù)訪問層的實現(xiàn)步驟

    Spring Data MongoDB構建高效數(shù)據(jù)訪問層的實現(xiàn)步驟

    本文主要介紹了Spring Data MongoDB構建高效數(shù)據(jù)訪問層的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2026-04-04
  • Java實現(xiàn)簡單小畫板

    Java實現(xiàn)簡單小畫板

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單小畫板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 詳解Spring框架注解掃描開啟之配置細節(jié)

    詳解Spring框架注解掃描開啟之配置細節(jié)

    本篇文章主要介紹了詳解Spring框架注解掃描開啟之配置細節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • jar包啟動時如何讀取配置文件優(yōu)先順序

    jar包啟動時如何讀取配置文件優(yōu)先順序

    SpringBoot啟動時讀取配置文件有優(yōu)先順序,從高到低為:bat文件目錄/config、bat文件目錄、classpath/config、classpath,可以通過spring.config.location指定配置位置,覆蓋默認順序,優(yōu)先級高的配置會覆蓋低的配置
    2026-05-05
  • SWT(JFace)體驗之FormLayout布局

    SWT(JFace)體驗之FormLayout布局

    SWT(JFace)體驗之FormLayout布局示例代碼。
    2009-06-06
  • Java?源碼重讀系列之?HashMap

    Java?源碼重讀系列之?HashMap

    這篇文章主要為大家介紹了Java源碼重讀系列之HashMap示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Java用鄰接表存儲圖的示例代碼

    Java用鄰接表存儲圖的示例代碼

    鄰接表是圖的一種鏈式存儲方法,其數(shù)據(jù)結構包括兩部分:節(jié)點和鄰接點。本文將用鄰接表實現(xiàn)存儲圖,感興趣的小伙伴可以了解一下
    2022-06-06

最新評論

行唐县| 都安| 凤台县| 裕民县| 客服| 隆昌县| 晋中市| 平谷区| 措勤县| 萨迦县| 七台河市| 谷城县| 四川省| 封丘县| 萨嘎县| 海原县| 公安县| 汉沽区| 长兴县| 合水县| 大同县| 桦甸市| 沅陵县| 阿城市| 孝感市| 汉川市| 讷河市| 两当县| 静海县| 汉中市| 石河子市| 高阳县| 瑞金市| 游戏| 云阳县| 高台县| 日喀则市| 安阳市| 六枝特区| 谢通门县| 金沙县|