今天要聊的就是「博客管理」中全文搜索的實(shí)現(xiàn),基于 SpringBoot+Vue+ES 實(shí)現(xiàn),先給大家看一下效果:
![]()
全文搜索+關(guān)鍵字高亮,是不是和百度的效果差不多,話不多說(shuō),直接聊如何實(shí)現(xiàn)。
該網(wǎng)站前端是仿一個(gè)開(kāi)源項(xiàng)目,本人主要是做后端,所以本教程涉及前端的部分會(huì)直接給代碼,不做深入講解數(shù)據(jù)準(zhǔn)備
首先我們的數(shù)據(jù)是存放在 MySQL 的,建表語(yǔ)句如下,再利用 MyBatis-Plus 實(shí)現(xiàn)普通查詢,比較簡(jiǎn)單,也不是本文重點(diǎn),不做贅述。
create table code_note ( id bigint not null auto_increment, title varchar(128) not null default '' comment '標(biāo)題', md_content text comment 'md文本', html_content text comment 'html文本', summary varchar(256) comment '摘要', category varchar(10) comment '分類', type tinyint not null default 0 comment '類型:0-文字;1-視頻', create_time datetime not null comment '創(chuàng)建時(shí)間', publish_time datetime not null comment '發(fā)布時(shí)間', status tinyint DEFAULT 0 COMMENT '0-草稿箱;1-已發(fā)表;2-已刪除', primary key (id) ); INSERT INTO yitiao_admin.code_note (id, title, author, md_content, html_content, summary, category, type, create_time, publish_time, status) VALUES (14, '一條', 'yitiao', 'canal', '
canal
', null, '默認(rèn)', null, '2023-01-30 10:28:17', '2023-01-30 10:28:17', 1); 復(fù)制代碼 前端頁(yè)面
前端是基于 element-ui 來(lái)實(shí)現(xiàn)的,從文檔找了半天,決定用 table 來(lái)實(shí)現(xiàn),如果有更好的實(shí)現(xiàn)方式可以評(píng)論區(qū)留言。
其實(shí)就是只有的一列的無(wú)邊框的表格,表格內(nèi)又嵌入文本和按鈕,再就是一些樣式的調(diào)整,關(guān)鍵代碼如下:
border >
margin-top: 5px;font-size: medium" v-html="scope.row.esContent">
{{ scope.row.author }} {{ scope.row.createTime }}
復(fù)制代碼
「查詢」和「全文搜索」按鈕的切換使用的 v-if="searchShow",向后端發(fā)請(qǐng)求的部分如下:
fullSearch() { this.searchShow = true; this.pageShow = false; if (this.search === '' || this.Search === null) { this.search = 'spring' } request.get("/es/note/getByContent/", { params: { // pageNum: this.currentPage, // pageSize: this.pageSize, content: this.search } }).then(res => { console.log(res) this.searchTableData = res.data }) } 復(fù)制代碼 Docker安裝ES
終于要到正題啦,因?yàn)镋S非常的耗內(nèi)存,我的服務(wù)器剩余內(nèi)存只有不到2G,所以選擇用Docker部署單機(jī)版的ES。
sudo docker pull ElasticSearch:7.12.0 ## 創(chuàng)建掛載目錄 config、data、plugins,開(kāi)啟全部權(quán)限 chmod -R 777 /data/opt/es ## 創(chuàng)建配置文件 cd config vim elasticsearch.yml http.host: 0.0.0.0 ## 啟動(dòng)容器 sudo docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms84m -Xmx512m" -v /data/opt/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /data/opt/es/data:/usr/share/elasticsearch/data -v /data/opt/es/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.12.0 # 查看日志 docker logs elasticsearch 復(fù)制代碼
測(cè)試正常啟動(dòng)頁(yè)面:http://101.43.138.173:9200/
插件使用
![]()
集群黃色解決
![]()
我們的elasticsearch是單節(jié)點(diǎn)的,只有一個(gè)主服務(wù)沒(méi)有從服務(wù),也就是說(shuō)所以最簡(jiǎn)單的辦法就是在創(chuàng)建索引的時(shí)候?qū)浞輸?shù)改為0。
如果我們已經(jīng)創(chuàng)建了索引,那么我們可以直接更改索引的備份數(shù)方法舉例如下:
## 請(qǐng)求方式為put ## url地址解釋:IP地址:端口/索引名稱/_settings(_settings 是接口的固定用法) curl -X PUT -H "Content-Type: Application/json" -d '{"number_of_replicas":0}' http://101.43.138.173:9200/Code_note/_settings --user name:password ## 返回 {"acknowledged":true} 復(fù)制代碼
刷新插件,集群變成綠色。
設(shè)置用戶名密碼
# vim elasticsearch.yml http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization xpack.security.enabled: true xpack.security.transport.ssl.enabled: true 復(fù)制代碼 docker exec -it fa41ca453d06 /bin/bash ./bin/elasticsearch-setup-passwords interactive ## 輸入密碼 復(fù)制代碼
![]()
設(shè)置成功后,用戶名為elastic,密碼為設(shè)置的值,同時(shí)es里多了一個(gè)索引:.security-7
![]()
安裝分詞器
下載,版本一定要和es的對(duì)應(yīng),安裝時(shí)注意,并不是一解壓就好了。
首先查看插件的名字,解壓后打開(kāi)plugin-descriptor.properties文件,查看插件的名字,然后在掛載的plugins文件夾下新建文件夾,以插件的名字命名。
再將解壓出來(lái)文件全部移動(dòng)到插件名文件夾下才可以。
![]()
重啟ES,查看日志
docker restart fa41ca453d06 docker logs fa41ca453d06 復(fù)制代碼
至此,ES服務(wù)端部署完成,接下來(lái)就是基于SpringBoot操作ES。
Java客戶端
spring-boot-starter-data-elasticsearch是比較好用的一個(gè)elasticsearch客戶端,它內(nèi)部會(huì)引入spring-data-elasticsearch。
版本對(duì)應(yīng)關(guān)系
如果使用spring-boot-starter-data-elasticsearch,需要調(diào)整spring-boot的版本才起作用。
![]()
有下邊這幾種方法操作ElasticSearch:
- ElasticsearchRepository(傳統(tǒng)的方法,可以使用)
- ElasticsearchRestTemplate(推薦使用。基于RestHighLevelClient)
- ElasticsearchTemplate(ES7中廢棄,不建議使用。基于TransportClient)
- RestHighLevelClient(推薦度低于ElasticsearchRestTemplate,因?yàn)锳PI不夠高級(jí))
- TransportClient(ES7中廢棄,不建議使用)
案例代碼
配置
org.springframework.boot spring-boot-starter-data-elasticsearch 2.7.7 復(fù)制代碼 spring: elasticsearch: rest: uris: 101.43.138.173:9200 # 多個(gè)用逗號(hào)隔開(kāi) # username: ---用戶名 # password: ---密碼 connection-timeout: 1000 # 連接超時(shí)時(shí)間 read-timeout: 1000 # 讀取超時(shí)時(shí)間 復(fù)制代碼
索引類
// 省略部分字段 @Data @Document(indexName = "code_note") @Setting(replicas = 0) // 副本為0,單機(jī)模式 @NoArgsConstructor @AllArgsConstructor @Builder public class EsCodeNote { @Id private Long id; /** * md文本 */ @Field(type = FieldType.Text, analyzer = "ik_max_word") private String mdContent; /** * 分類 */ @Field(type = FieldType.Keyword) private String category; /** * 創(chuàng)建時(shí)間 */ @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis") private Date createTime; } 復(fù)制代碼
mapper類
@Repository public interface CodeNoteRepository extends ElasticsearchRepository { } 復(fù)制代碼
service層
@Service @Slf4j @RequiredArgsConstructor public class CodeNoteService { private final ElasticsearchRestTemplate esRestTemplate; private final CodeNoteRepository codeNoteRepository; private final CodeNoteMapper noteMapper; public Object saveNoteToEs(EsCodeNote codeNote){ return codeNoteRepository.save(codeNote); } public void saveNotesToEs(List codeNotes){ codeNoteRepository.saveAll(codeNotes); } public List getFromEsByContent(String content) { //高亮 String preTag = ""; String postTag = ""; boolQueryBuilder boolQueryBuilder = new BoolQueryBuilder().should(new MatchQueryBuilder("mdContent", content)); Query query = new NativeSearchQueryBuilder() .withQuery(boolQueryBuilder) .withHighlightFields(new HighlightBuilder.Field("mdContent").preTags(preTag).postTags(postTag)).build(); // Query query1 = new NativeSearchQueryBuilder() // .withQuery(QueryBuilders.multiMatchQuery(content,"content","content.inner")).build(); // .withQuery(QueryBuilders.queryStringQuery(content)).build(); SearchHits search = esRestTemplate.search(query, EsCodeNote.class); return search.stream().map(SearchHit::getContent).collect(Collectors.toList()); } public void init() { List codeNotes = noteMapper.selectList(Wrappers.lambdaQuery(CodeNote.class)); List esCodeNotes = BeanUtil.copyToList(codeNotes, EsCodeNote.class); this.saveNotesToEs(esCodeNotes); } } 復(fù)制代碼
controller
@RestController @RequestMapping("/es") @Slf4j @RequiredArgsConstructor public class EsRestController { private final CodeNoteService noteService; @PostMapping("/init") public Result createIndex() { noteService.init(); return Result.success("init all notes success"); } @GetMapping("/note/getByContent") public Result> getByContent(@RequestParam("content")String content) { return Result.success(noteService.getFromEsByContent(content)); } } 復(fù)制代碼
測(cè)試
先初始化全部數(shù)據(jù)
![]()
根據(jù)mdContent分詞查詢
![]()
至此后端的高亮查詢已經(jīng)實(shí)現(xiàn),如果與前端結(jié)合,還需要對(duì)查詢結(jié)果做進(jìn)一步封裝和處理。
前后端聯(lián)調(diào)
后端構(gòu)建返回VO
public class EsCodeNoteRes { private Long id; /** * 題目 */ private String esTitle; private String author; /** * md文本 */ private String esContent; /** * html文本 */ private String htmlContent; // 省略部分 /** * 發(fā)布時(shí)間 */ @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8") private Date publishTime; } 復(fù)制代碼
對(duì)返回的結(jié)果封裝
SearchHits searchHits = esRestTemplate.search(query, EsCodeNote.class); return searchHits.stream().map(search -> { EsCodeNote esCodeNote = search.getContent(); search.getHighlightFields().forEach((k, v) -> { log.info("highlight key is [{}],content is [{}]", k, v.get(0)); // 分別處理標(biāo)題和正文 if (k.equals("title")) { esCodeNote.setTitle(v.get(0)); } if (k.equals("mdContent")) { esCodeNote.setMdContent(v.get(0)); } }); // 如果正文里沒(méi)有關(guān)鍵字,取前100字符 if (!esCodeNote.getMdContent().contains(postTag)){ esCodeNote.setMdContent(esCodeNote.getMdContent().substring(0,100)); } return EsCodeNoteRes.builder() .id(esCodeNote.getId()) .esTitle(esCodeNote.getTitle()) .author(esCodeNote.getAuthor()) .esContent(esCodeNote.getMdContent()) .htmlContent(esCodeNote.getHtmlContent()) .summary(esCodeNote.getSummary()) .category(esCodeNote.getCategory()) .createTime(esCodeNote.getCreateTime()) .publishTime(esCodeNote.getPublishTime()) .build(); }).collect(Collectors.toList()); 復(fù)制代碼
結(jié)果展示
![]()
道阻且長(zhǎng),行則將至。2023,揚(yáng)帆起航。
原文鏈接:https://juejin.cn/post/7194734486327099429






