【牛客讨论区】第六章:Elasticsearch
目錄
- 1. Elasticsearch入門
- 2. Spring整合Elasticsearch
- 2.1 springboot 版本問題
- 2.2 整合Elasticsearch
- 3. 開發社區搜索功能
1. Elasticsearch入門
Elasticsearch簡介
- 一個分布式的、Restful風格的搜索引擎。
- 支持對各種類型的數據的檢索。
- 搜索速度快,可以提供實時的搜索服務。
- 便于水平擴展,每秒可以處理PB級海量數據。
Elasticsearch術語
- 索引、類型、文檔、字段。
- 集群、節點、分片、副本。
https://www.elastic.co
https://www.getpostman.com
【安裝】
解壓 elasticsearch-6.4.3.zip 即可
【修改配置文件 】
E:\elasticsearch-6.4.3\config 下的 elasticsearch.yml
17行
cluster.name: nowcoder33行
path.data: d:\work\data\elasticsearch-6.4.3\data37行
path.logs: d:\work\data\elasticsearch-6.4.3\logs【配置環境變量】
將 bin 目錄 E:\elasticsearch-6.4.3\bin 配到 path 中
【安裝中文分詞插件】
注意:必須將elasticsearch-analysis-ik-6.4.3.zip 解壓到固定的目錄下
E:\elasticsearch-6.4.3\plugins 下新建 ik 目錄,解壓到 ik 目錄下即可
【安裝postman】
【啟動 elasticsearch】
Windows下,雙擊 E:\elasticsearch-6.4.3\bin 下的 elasticsearch.bat
打開一個 cmd,訪問 elasticsearch 健康狀況
curl -X GET "localhost:9200/_cat/health?v"查看節點
curl -X GET "localhost:9200/_cat/nodes?v"查看索引
curl -X GET "localhost:9200/_cat/indices?v"創建索引
curl -X PUT "localhost:9200/test"刪除索引
curl -X DELETE "localhost:9200/test"【使用 postman 訪問 es】
發送 GET 請求:查看索引
localhost:9200/_cat/indices?v
發送 PUT 請求:建立索引
localhost:9200/test
發送 DELETE 請求:刪除索引
localhost:9200/test
【向 es 提交數據】
test 是索引,也是表名,不存在的話會自動創建,_doc是占位的,1是 id,在 body 中寫數據,以 JSON 方式傳輸
點擊 send,返回結果:
{"_index": "test","_type": "_doc","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1 }【查看該條數據】
發送 GET 請求:localhost:9200/test/_doc/1
【刪除該數據】
發送 DELETE 請求:localhost:9200/test/_doc/1
【實現搜索】
先提交幾條數據
PUT : localhost:9200/test/_doc/1
{"title":"互聯網求職","content":"尋求一份運維的崗位" }PUT : localhost:9200/test/_doc/2
{"title":"互聯網招聘","content":"招一名資深程序員" }PUT : localhost:9200/test/_doc/3
{"title":"實習生推薦","content":"本人在一家互聯網公司任職,可推薦實習開發崗位!" }搜索:
不加條件的搜索
GET:localhost:9200/test/_search
加條件的搜索
GET:localhost:9200/test/_search?q=title:互聯網
localhost:9200/test/_search?q=content:運維實習
搜索引擎會將 “運維實習” 分詞成 “運維”和 “實習”,分別去搜索。
【更加復雜的搜索】
地址欄寫不下,可以在 body 中寫,fields 表示要在哪些字段中進行搜索
2. Spring整合Elasticsearch
2.1 springboot 版本問題
【問題】
我的 springboot 版本是 2.3.7.RELEASE,需要降到 2.1.5.RELEASE,不然和 Elasticsearch 版本不兼容,會產生一系列的問題
【解決】
將 pom.xml 中所有的 2.3.7.RELEASE 都改成 2.1.5.RELEASE,然后點擊IDEA 的 “File”–“Invalidate Caches” ,重新啟動即可
2.2 整合Elasticsearch
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>application.properties
# ElasticsearchProperties spring.data.elasticsearch.cluster-name=nowcoder spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300【解決沖突】
Redis 和 elasticsearch 底層都是基于 Netty,所以二者啟動時會有沖突,解決如下:
CommunityApplication 類的 main 方法之前寫個方法
@PostConstruct public void init() {//解決Netty啟動沖突//詳見 Netty4Utils 類,設置為falseSystem.setProperty("es.set.netty.runtime.available.processors", "false"); }因為我們是將帖子存到 es 中實現搜索功能,所以需要通過注解配置一下 DiscussPost 類
@Document(indexName = "discusspost", type = "_doc", shards = 6, replicas = 3) public class DiscussPost {@Idprivate int id;@Field(type = FieldType.Integer)private int userId;//analyzer:存儲分詞器,searchAnalyzer:搜索分詞器@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")private String title;@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")private String content;@Field(type = FieldType.Integer)private int type;@Field(type = FieldType.Integer)private int status;@Field(type = FieldType.Date)private Date createTime;@Field(type = FieldType.Integer)private int commentCount;@Field(type = FieldType.Double)private double score;dao 包下新建子包 elasticsearch
新建接口
新建測試類
最終版
package com.nowcoder.community;import com.nowcoder.community.dao.DiscussPostMapper; import com.nowcoder.community.dao.elasticsearch.DiscussPostRepository; import com.nowcoder.community.entity.DiscussPost; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.SearchResultMapper; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List;@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = CommunityApplication.class) public class ElasticsearchTests {@Resourceprivate DiscussPostMapper discussPostMapper;@Resourceprivate DiscussPostRepository discussPostRepository;//DiscussPostRepository 功能有限,因此還需要 ElasticsearchTemplate@Resourceprivate ElasticsearchTemplate elasticTemplate;@Testpublic void testInsert() { //插入一條數據discussPostRepository.save(discussPostMapper.selectDiscussById(241));discussPostRepository.save(discussPostMapper.selectDiscussById(242));discussPostRepository.save(discussPostMapper.selectDiscussById(243));}@Testpublic void testInsertList() { //插入多條數據。為后面的搜索功能做準備discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(101, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(102, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(103, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(112, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(131, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(132, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(133, 0, 100));discussPostRepository.saveAll(discussPostMapper.selectDiscussPosts(134, 0, 100));}@Testpublic void testUpdate() { //所謂修改就是save一下,覆蓋原來的DiscussPost post = discussPostMapper.selectDiscussById(231);post.setContent("我是新人,使勁灌水...");discussPostRepository.save(post);}@Testpublic void testDelete() {discussPostRepository.deleteById(231); //刪一條數據//discussPostRepository.deleteAll();//刪除所有數據}@Testpublic void testSearchByRepository() {SearchQuery searchQuery = new NativeSearchQueryBuilder()//搜索條件 從title和content中搜 互聯網寒冬.withQuery(QueryBuilders.multiMatchQuery("互聯網寒冬", "title", "content"))//排序條件.withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) //優先按照type降序.withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)).withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)).withPageable(PageRequest.of(0, 10)).withHighlightFields(new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")).build();//底層獲取到了高亮顯示的值,但是沒有返回Page<DiscussPost> page = discussPostRepository.search(searchQuery);System.out.println(page.getTotalElements());System.out.println(page.getTotalPages());System.out.println(page.getNumber());System.out.println(page.getSize());for (DiscussPost post : page) {System.out.println(post);}}@Testpublic void testSearchByTemplate() {SearchQuery searchQuery = new NativeSearchQueryBuilder()//搜索條件 從title和content中搜 互聯網寒冬.withQuery(QueryBuilders.multiMatchQuery("互聯網寒冬", "title", "content"))//排序條件.withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) //優先按照type降序.withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)).withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)).withPageable(PageRequest.of(0, 10)).withHighlightFields(new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")).build();Page<DiscussPost> page = elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() {@Overridepublic <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {SearchHits hits = response.getHits();if (hits.getTotalHits() <= 0) {return null;}List<DiscussPost> list = new ArrayList<>();for (SearchHit hit : hits) {DiscussPost post = new DiscussPost();String id = hit.getSourceAsMap().get("id").toString();post.setId(Integer.valueOf(id));String userId = hit.getSourceAsMap().get("userId").toString();post.setUserId(Integer.valueOf(userId));String title = hit.getSourceAsMap().get("title").toString();post.setTitle(title);String content = hit.getSourceAsMap().get("content").toString();post.setContent(content);String status = hit.getSourceAsMap().get("status").toString();post.setStatus(Integer.valueOf(status));String createTime = hit.getSourceAsMap().get("createTime").toString();post.setCreateTime(new Date(Long.valueOf(createTime)));String commentCount = hit.getSourceAsMap().get("commentCount").toString();post.setCommentCount(Integer.valueOf(commentCount));//處理高亮顯示的結果HighlightField titleField = hit.getHighlightFields().get("title");if (titleField != null) {post.setTitle(titleField.getFragments()[0].toString());}HighlightField contentField = hit.getHighlightFields().get("content");if (contentField != null) {post.setContent(contentField.getFragments()[0].toString());}list.add(post);}return new AggregatedPageImpl(list, pageable,hits.getTotalHits(), response.getAggregations(),response.getScrollId(), hits.getMaxScore());}});System.out.println(page.getTotalElements());System.out.println(page.getTotalPages());System.out.println(page.getNumber());System.out.println(page.getSize());for (DiscussPost post : page) {System.out.println(post);}} }啟動 zookeeper、kafka、Elasticsearch,執行測試方法 testInsert()
【踩坑】
這里執行了好多次,每次都出現 java: 程序包org.junit.jupiter.api不存在
原因是我們降低 springboot 版本之后,Junit 的版本也由 5 降低為 4,org.junit.jupiter.api 是 Junit5 的,自然就找不到了,所以我們需要按照 Junit4 的語法進行測試,org.junit.Test 是Junit4 的包
方法:
將所有測試類中的 import org.junit.jupiter.api.Test; 改成 import org.junit.Test;在所有測試類的類名前寫這三個注解(BlockingQueueTests不用管,因為他是用main方法測試的)
如果還有錯,重啟一下 elasticsearch
使用 postman 查看是否插入成功:
3. 開發社區搜索功能
搜索服務
- 將帖子保存至Elasticsearch服務器。
- 從Elasticsearch服務器刪除帖子。
- 從Elasticsearch服務器搜索帖子。
發布事件
- 發布帖子時,將帖子異步的提交到Elasticsearch服務器。
- 增加評論時,將帖子異步的提交到Elasticsearch服務器。
- 在消費組件中增加一個方法,消費帖子發布事件。
顯示結果
- 在控制器中處理搜索請求,在HTML上顯示搜索結果。
解決一個遺漏的小問題:
discusspost-mapper.xml,加上 keyProperty
service層
package com.nowcoder.community.service;import com.nowcoder.community.dao.elasticsearch.DiscussPostRepository; import com.nowcoder.community.entity.DiscussPost; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.SearchResultMapper; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.stereotype.Service;import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List;@Service public class ElasticsearchService {@Resourceprivate DiscussPostRepository discussRepository;@Resourceprivate ElasticsearchTemplate elasticTemplate;public void saveDiscussPost(DiscussPost post) {discussRepository.save(post);}public void deleteDiscussPost(int id) {discussRepository.deleteById(id);}public Page<DiscussPost> searchDiscussPost(String keyword, int current, int limit) {SearchQuery searchQuery = new NativeSearchQueryBuilder()//搜索條件 從title和content中搜 互聯網寒冬.withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content"))//排序條件.withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) //優先按照type降序.withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)).withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)).withPageable(PageRequest.of(current, limit)).withHighlightFields(new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")).build();return elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() {@Overridepublic <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {SearchHits hits = response.getHits();if (hits.getTotalHits() <= 0) {return null;}List<DiscussPost> list = new ArrayList<>();for (SearchHit hit : hits) {DiscussPost post = new DiscussPost();String id = hit.getSourceAsMap().get("id").toString();post.setId(Integer.valueOf(id));String userId = hit.getSourceAsMap().get("userId").toString();post.setUserId(Integer.valueOf(userId));String title = hit.getSourceAsMap().get("title").toString();post.setTitle(title);String content = hit.getSourceAsMap().get("content").toString();post.setContent(content);String status = hit.getSourceAsMap().get("status").toString();post.setStatus(Integer.valueOf(status));String createTime = hit.getSourceAsMap().get("createTime").toString();post.setCreateTime(new Date(Long.valueOf(createTime)));String commentCount = hit.getSourceAsMap().get("commentCount").toString();post.setCommentCount(Integer.valueOf(commentCount));//處理高亮顯示的結果HighlightField titleField = hit.getHighlightFields().get("title");if (titleField != null) {post.setTitle(titleField.getFragments()[0].toString());}HighlightField contentField = hit.getHighlightFields().get("content");if (contentField != null) {post.setContent(contentField.getFragments()[0].toString());}list.add(post);}return new AggregatedPageImpl(list, pageable,hits.getTotalHits(), response.getAggregations(),response.getScrollId(), hits.getMaxScore());}});}}controller 層
CommunityConstant
新增常量
DiscussPostController 完善 addDiscussPost() 方法
@Resource private EventProducer eventProducer;@PostMapping("/add") @ResponseBody public String addDiscussPost(String title, String content) {User user = hostHolder.getUser();if (user == null) {return CommunityUtil.getJSONString(403, "你還沒有登錄哦!");}DiscussPost discussPost = new DiscussPost();discussPost.setUserId(user.getId());discussPost.setTitle(title);discussPost.setContent(content);discussPost.setCreateTime(new Date());discussPostService.addDiscussPost(discussPost);//觸發發帖事件Event event = new Event().setTopic(TOPIC_PUBLISH).setUserId(user.getId()).setEntityType(ENTITY_TYPE_POST).setEntityId(discussPost.getId());eventProducer.fireEvent(event);//報錯的情況,將來統一處理return CommunityUtil.getJSONString(0, "發布成功!"); }CommentController
addComment 方法,在 return 之前,加一段邏輯:
某個帖子新增評論之后,需要重新上傳 es 服務器,覆蓋掉舊的帖子
EventConsumer
增加方法
@Resource private DiscussPostService discussPostService;@Resource private ElasticsearchService elasticsearchService;//消費發帖事件 @KafkaListener(topics = {TOPIC_PUBLISH}) public void handlePublishMessage(ConsumerRecord record) {if (record == null || record.value() == null) {logger.error("消息的內容為空!");return;}Event event = JSONObject.parseObject(record.value().toString(), Event.class);if (event == null) {logger.error("消息格式錯誤!");return;}DiscussPost post = discussPostService.findDiscussPostById(event.getEntityId());elasticsearchService.saveDiscussPost(post); }新建controller
package com.nowcoder.community.controller;import com.nowcoder.community.entity.DiscussPost; import com.nowcoder.community.entity.Page; import com.nowcoder.community.service.ElasticsearchService; import com.nowcoder.community.service.LikeService; import com.nowcoder.community.service.UserService; import com.nowcoder.community.util.CommunityConstant; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;@Controller public class SearchController implements CommunityConstant {@Resourceprivate ElasticsearchService elasticsearchService;@Resourceprivate UserService userService;@Resourceprivate LikeService likeService;//格式 search?keyword=xxx@GetMapping("/search")public String search(String keyword, Page page, Model model) {//搜索帖子org.springframework.data.domain.Page<DiscussPost> searchResult =elasticsearchService.searchDiscussPost(keyword, page.getCurrent() - 1, page.getLimit());//聚合數據List<Map<String, Object>> discussPosts = new ArrayList<>();if (searchResult != null) {for (DiscussPost post : searchResult) {Map<String, Object> map = new HashMap<>();//帖子map.put("post", post);//作者map.put("user", userService.findUserById(post.getUserId()));//點贊數量map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));discussPosts.add(map);}}model.addAttribute("discussPosts", discussPosts);model.addAttribute("keyword", keyword);//分頁page.setPath("/search?keyword=" + keyword);page.setRows(searchResult == null ? 0 : (int) searchResult.getTotalElements());return "/site/search";} }index.html
52行 form 表單
<!-- 搜索 --> <form class="form-inline my-2 my-lg-0" method="get" th:action="@{/search}"><input class="form-control mr-sm-2" type="search" aria-label="Search" name="keyword" th:value="${keyword}"/><button class="btn btn-outline-light my-2 my-sm-0" type="submit">搜索</button> </form>search.html
2行
8行
<link rel="stylesheet" th:href="@{/css/global.css}" />14行
<header class="bg-dark sticky-top" th:replace="index::header">刪除 86-161 的 li 標簽,留一個 li 即可
170行
<script th:src="@{/js/global.js}"></script>67行
<li class="media pb-3 pt-3 mb-3 border-bottom" th:each="map:${discussPosts}">68行
<img th:src="${map.user.headerUrl}" class="mr-4 rounded-circle" alt="用戶頭像" style="width: 50px;height: 50px;">71行
<a th:href="@{|/discuss/detail/${map.post.id}|}" th:utext="${map.post.title}">備戰<em>春招</em>,面試刷題跟他復習,一個月全搞定!</a>73行
<div class="mb-3" th:utext="${map.post.content}">77行
<u class="mr-3" th:utext="${map.user.username}">寒江雪</u> 發布于 <b th:text="${#dates.format(map.post.createTime, 'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>80行
<li class="d-inline ml-2">贊 <i th:text="${map.likeCount}">11</i></li>82行
<li class="d-inline ml-2">回復 <i th:text="${map.post.commentCount}">7</i></li>89行
<!-- 分頁 --> <nav class="mt-5" th:replace="index::pagination">測試:
登錄賬號,新發一個帖子,看看能不能搜到。
總結
以上是生活随笔為你收集整理的【牛客讨论区】第六章:Elasticsearch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一辆车到底需要多少芯片?
- 下一篇: 建立一个GTalk连接和启动一个IM会话