springboot elasticsearch vue ik中文分词器 实现百度/京东全文搜索
生活随笔
收集整理的這篇文章主要介紹了
springboot elasticsearch vue ik中文分词器 实现百度/京东全文搜索
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
背景:實現(xiàn)和百度搜索一樣效果的,全文搜索引擎支持關(guān)鍵詞高亮顯示
文章目錄
- 1. 企業(yè)級搜索引擎解決方案
- 2. 創(chuàng)建索引規(guī)則
- 3. 數(shù)據(jù)拉取
- 4. 搜索高亮
- 5. 自定義詞庫
- 6. 效果圖
- 7. 開源源碼
1. 企業(yè)級搜索引擎解決方案
分詞器:english、standard、ik_max_smart、ik_smart、whitespace等
2. 創(chuàng)建索引規(guī)則
PUT /jd_goods {"settings" : {"number_of_shards" : 1,"number_of_replicas" : 1},"mappings": {"properties": {"id":{"type": "integer"},"title":{"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"desc":{"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"price":{"type": "text"},"img":{"type": "text"}}} }3. 數(shù)據(jù)拉取
http://localhost:9090/parse/vue
public List<Content> parseJD(String keyword) throws IOException {String baseUrl = "https://search.xxx.com/Search?keyword=";// 解析網(wǎng)頁 在線預(yù)覽中有處理Document document = Jsoup.parse(new URL(baseUrl + keyword), 30000);// 所有js中可以使用的方法,這里都能用Element element = document.getElementById("J_goodsList");//獲取搜有的li元素Elements elements = element.getElementsByTag("li");ArrayList<Content> goodList = new ArrayList<>();// 獲取元素中的內(nèi)容,這里el 就是每一個li標(biāo)簽了for (Element el : elements) {//關(guān)于這種圖片特別多的網(wǎng)站,所有的圖片都是拉加載的String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");String price = el.getElementsByClass("p-price").eq(0).text();String title = el.getElementsByClass("p-name").eq(0).text();Content content = new Content();content.setTitle(title);content.setImg(img);content.setPrice("內(nèi)容標(biāo)題 " + price);content.setDesc("內(nèi)容描述 " + title);goodList.add(content);}return goodList;}4. 搜索高亮
// 2. 獲取es中的數(shù)據(jù),實現(xiàn)基本搜索高亮功能public List<Map<String, Object>> searchPageHighlight(String keyword, int pageNo, int pageSize) throws IOException {if (pageNo <= 1) {pageNo = 1;}// 條件搜索SearchRequest searchRequest = new SearchRequest(ESConst.JD_SEARCH_INDEX);SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 分頁searchSourceBuilder.from(pageNo);searchSourceBuilder.size(pageSize);// 精準(zhǔn)匹配TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(ESConst.SEARCH_CONDITION_FIELD, keyword);searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));//構(gòu)建高亮HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field(ESConst.HIGHLIGHT_TITLE);highlightBuilder.field("desc");highlightBuilder.requireFieldMatch(true);//多個高亮 顯示highlightBuilder.preTags(ESConst.HIGHLIGHT_PRE_TAGS);highlightBuilder.postTags(ESConst.HIGHLIGHT_POST_TAGS);searchSourceBuilder.highlighter(highlightBuilder);// 執(zhí)行搜索searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);// 解析結(jié)果ArrayList<Map<String, Object>> list = new ArrayList<>();for (SearchHit hit : searchResponse.getHits().getHits()) {// 解析高亮的字段,將原來的字段置換為我們高亮的字段即可!Map<String, HighlightField> highlightFields = hit.getHighlightFields();HighlightField title = highlightFields.get(ESConst.HIGHLIGHT_TITLE);HighlightField desc = highlightFields.get("desc");// 獲取原來的結(jié)果Map<String, Object> sourceAsMap = hit.getSourceAsMap();if (title != null) {Text[] fragments = title.fragments();String newTitle = "";for (Text text : fragments) {newTitle += text;}//高亮字段替換掉原來的內(nèi)容即可sourceAsMap.put(ESConst.SEARCH_CONDITION_FIELD, newTitle);}if (desc != null) {Text[] fragments = desc.fragments();String newDesc = "";for (Text text : fragments) {newDesc += text;}//高亮字段替換掉原來的內(nèi)容即可sourceAsMap.put("desc", newDesc);}// 將結(jié)果放入list容器返回list.add(sourceAsMap);}return list;}5. 自定義詞庫
new_word.dic
女包 女士包 java語言6. 效果圖
http://localhost:9090/
7. 開源源碼
https://gitee.com/gblfy/es7-jd-vue
總結(jié)
以上是生活随笔為你收集整理的springboot elasticsearch vue ik中文分词器 实现百度/京东全文搜索的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue Bootstrap OSS 实现
- 下一篇: nginx绑定多个端口