ElasticSearch 分詞器

文章目錄

      • 一、安裝中文分詞插件
        • Linux安裝7.14.1版本:
          • 測試1:ik_smart
          • 測試2:ik_max_word
      • 二、es內置的分詞器:
      • 三、拼音插件安裝以及(IK+pinyin使用)
        • 配置 IK + pinyin 分詞配置

一、安裝中文分詞插件

IK Analysis for Elasticsearch是開源社區比較流行的中文分詞插件
官網:https://github.com/medcl/elasticsearch-analysis-ik

??本來想用這兩種方法安裝:

.\bin\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.4.2/elasticsearch-analysis-ik-5.4.2.zip
bin\elasticsearch-plugin install file:///C:\Users\jk\Desktop\elasticsearch-analysis-ik-5.4.2.zip

??但是卻總是報錯:

ERROR: `elasticsearch` directory is missing in the plugin zip

??后來只能用了這種方法:在Elasticsearch安裝目錄下的文件夾plugins中新建文件夾ik,將elasticsearch-analysis-ik-5.4.2.zip解壓到這里即可,其實官網里已經說明了低于5.5.1版本的用解壓的方式安裝了:
在這里插入圖片描述
參考:Elasticsearch5.x安裝IK分詞器以及使用

Linux安裝7.14.1版本:

下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

[hadoop@node01 ~]$ elasticsearch-7.14.1/bin/elasticsearch-plugin install file:///mnt/elasticsearch-analysis-ik-7.14.1.zip

在這里插入圖片描述
ik_smart:會做最粗粒度的拆分
ik_max_word:會將文本做最細粒度的拆分。

測試1:ik_smart
GET /_analyze
{"analyzer": "ik_smart","text":"中華人民共和國"
}

結果:

{"tokens": [{"token": "中華人民共和國","start_offset": 0,"end_offset": 7,"type": "CN_WORD","position": 0}]
}
測試2:ik_max_word
GET /_analyze
{"analyzer": "ik_max_word","text":"中華人民共和國"
}

結果:

{"tokens": [{"token": "中華人民共和國","start_offset": 0,"end_offset": 7,"type": "CN_WORD","position": 0},{"token": "中華人民","start_offset": 0,"end_offset": 4,"type": "CN_WORD","position": 1},{"token": "中華","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 2},{"token": "華人","start_offset": 1,"end_offset": 3,"type": "CN_WORD","position": 3},{"token": "人民共和國","start_offset": 2,"end_offset": 7,"type": "CN_WORD","position": 4},{"token": "人民","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 5},{"token": "共和國","start_offset": 4,"end_offset": 7,"type": "CN_WORD","position": 6},{"token": "共和","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 7},{"token": "國","start_offset": 6,"end_offset": 7,"type": "CN_CHAR","position": 8}]
}
GET /_analyze
{"analyzer": "ik_max_word","text":"I love you"
}
結果:
{"tokens" : [{"token" : "i","start_offset" : 0,"end_offset" : 1,"type" : "ENGLISH","position" : 0},{"token" : "love","start_offset" : 2,"end_offset" : 6,"type" : "ENGLISH","position" : 1},{"token" : "you","start_offset" : 7,"end_offset" : 10,"type" : "ENGLISH","position" : 2}]
}

參考:https://blog.csdn.net/wenxindiaolong061/article/details/82562450

二、es內置的分詞器:

  • standard analyzer
  • simple analyzer
  • whitespace analyzer
  • language analyzer(特定的語言的分詞器)

例句:Set the shape to semi-transparent by calling set_trans(5)
不同分詞器的分詞結果:

  • standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默認的是standard)
  • simple analyzer:set, the, shape, to, semi, transparent, by, calling, set, trans
  • whitespace analyzer:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
  • language analyzer(特定的語言的分詞器,比如說,english,英語分詞器):set, shape, semi, transpar, call, set_tran, 5

分詞器測試:

GET /_analyze
{"analyzer": "standard","text":"The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone."
}

結果:

  "tokens" : [{"token" : "the","start_offset" : 0,"end_offset" : 3,"type" : "<ALPHANUM>","position" : 0},{"token" : "2","start_offset" : 4,"end_offset" : 5,"type" : "<NUM>","position" : 1},{"token" : "quick","start_offset" : 6,"end_offset" : 11,"type" : "<ALPHANUM>","position" : 2},{"token" : "brown","start_offset" : 12,"end_offset" : 17,"type" : "<ALPHANUM>","position" : 3},{"token" : "foxes","start_offset" : 18,"end_offset" : 23,"type" : "<ALPHANUM>","position" : 4},{"token" : "jumped","start_offset" : 24,"end_offset" : 30,"type" : "<ALPHANUM>","position" : 5},{"token" : "over","start_offset" : 31,"end_offset" : 35,"type" : "<ALPHANUM>","position" : 6},{"token" : "the","start_offset" : 36,"end_offset" : 39,"type" : "<ALPHANUM>","position" : 7},{"token" : "lazy","start_offset" : 40,"end_offset" : 44,"type" : "<ALPHANUM>","position" : 8},{"token" : "dog’s","start_offset" : 45,"end_offset" : 50,"type" : "<ALPHANUM>","position" : 9},{"token" : "bone","start_offset" : 51,"end_offset" : 55,"type" : "<ALPHANUM>","position" : 10}]
}

??可以看出是按照空格、非字母的方式對輸入的文本進行了轉換,比如對 Java 做了轉小寫,對一些停用詞也沒有去掉,比如 in,其中 token 為分詞結果;start_offset 為起始偏移;end_offset 為結束偏移;position 為分詞位置。可配置項

選項描述
max_token_length最大令牌長度。如果看到令牌超過此長度,則將其max_token_length間隔分割。默認為255。
stopwords預定義的停用詞列表,例如english或包含停用詞列表的數組。默認為none。
stopwords_path包含停用詞的文件的路徑。
COPY{"settings": {"analysis": {"analyzer": {"my_english_analyzer": {"type": "standard","max_token_length": 5,"stopwords": "_english_"}}}}
}

??不同的 Analyzer 會有不同的分詞結果,內置的分詞器有以下幾種,基本上內置的 Analyzer 包括 Language Analyzers 在內,對中文的分詞都不夠友好,中文分詞需要安裝其它 Analyzer

分析器描述分詞對象結果
standard標準分析器是默認的分析器,如果沒有指定,則使用該分析器。它提供了基于文法的標記化(基于 Unicode 文本分割算法,如 Unicode 標準附件 # 29所規定) ,并且對大多數語言都有效。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog’s, bone ]
simple簡單分析器將文本分解為任何非字母字符的標記,如數字、空格、連字符和撇號、放棄非字母字符,并將大寫字母更改為小寫字母。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
whitespace空格分析器在遇到空白字符時將文本分解為術語The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog’s, bone. ]
stop停止分析器與簡單分析器相同,但增加了刪除停止字的支持。默認使用的是 english 停止詞。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
keyword不分詞,把整個字段當做一個整體返回The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.]
pattern模式分析器使用正則表達式將文本拆分為術語。正則表達式應該匹配令牌分隔符,而不是令牌本身。正則表達式默認為 w+ (或所有非單詞字符)。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
多種西語系 arabic, armenian, basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english等等一組旨在分析特定語言文本的分析程序。

??中文分詞器最簡單的是ik分詞器,還有jieba分詞,哈工大分詞器等

分析器描述分詞對象結果
ik_smartik分詞器中的簡單分詞器,支持自定義字典,遠程字典學如逆水行舟,不進則退[學如逆水行舟,不進則退]
ik_max_wordik_分詞器的全量分詞器,支持自定義字典,遠程字典學如逆水行舟,不進則退[學如逆水行舟,學如逆水,逆水行舟,逆水,行舟,不進則退,不進,則,退]

參考:
【9種】ElasticSearch分詞器詳解,一文get!!!| 博學谷狂野架構師
[轉]中英文停止詞表(stopword)

三、拼音插件安裝以及(IK+pinyin使用)

??有時在淘寶搜索商品的時候,會發現使用漢字、拼音、或者拼音混合漢字都會出來想要的搜索結果,其實是通過拼音搜索插件實現的。

??地址:https://github.com/infinilabs/analysis-pinyin/releases

??選擇對應的版本,版本與 ES 版本一致,建議直接下載編譯后的 zip 包;若是下載源碼包,則需要自己編碼打包 mvn clean package 生成 zip 包。聯網安裝:elasticsearch-7.14.1/bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/7.14.1

在這里插入圖片描述
??使用自定義拼音分詞器創建索引:

PUT /medcl/ 
{"settings" : {"analysis" : {"analyzer" : {"pinyin_analyzer" : {"tokenizer" : "my_pinyin"}},"tokenizer" : {"my_pinyin" : {"type" : "pinyin","keep_separate_first_letter" : false,"keep_full_pinyin" : true,"keep_original" : true,"limit_first_letter_length" : 16,"lowercase" : true,"remove_duplicated_term" : true}}}}
}

??測試:

POST medcl/_analyze
{"analyzer": "pinyin_analyzer","text": "劉德華"
}

??結果:

{"tokens" : [{"token" : "liu","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "劉德華","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "ldh","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "de","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 1},{"token" : "hua","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 2}]
}
配置 IK + pinyin 分詞配置

??settings 設置:

curl -XPUT "http://localhost:9200/medcl/" -d'
{"index": {"analysis": {"analyzer": {"default": {"tokenizer": "ik_max_word"},"pinyin_analyzer": {"tokenizer": "shopmall_pinyin"}},"tokenizer": {"shopmall_pinyin": {"keep_joined_full_pinyin": "true","keep_first_letter": "true","keep_separate_first_letter": "false","lowercase": "true","type": "pinyin","limit_first_letter_length": "16","keep_original": "true","keep_full_pinyin": "true"}}}}
}'

??創建 mapping:

curl -XPOST http://localhost:9200/medcl/folks/_mapping -d'
{"folks": {"properties": {"name": {"type": "text","analyzer": "ik_max_word","include_in_all": true,"fields": {"pinyin": {"type": "text","analyzer": "pinyin_analyzer"}}}}}
}'

??添加測試文檔:

curl -XPOST http://localhost:9200/medcl/folks/ -d'{"name":"劉德華"}'curl -XPOST http://localhost:9200/medcl/folks/ -d'{"name":"中華人民共和國國歌"}'

??拼音分詞效果:

curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:de"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh"

??中文分詞測試:

curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name:劉"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name:劉德"

注意:用戶輸入搜索內容,根據正則匹配分成中文、拼音、中文+拼音、中文+拼音+數字+特殊符號等情況進行搜索,如下:

  1. 若是漢字搜索,沒有搜索結果,轉化為拼音再搜索一次,按拼音搜索還是無結果,則按模糊搜索再搜一次,若是還無結果,可考慮推薦
  2. 若是拼音搜索,沒有搜索結果,則按模糊搜索再搜一次
  3. 若是漢字+拼音搜索,暫且按拼音處理
  4. 拼音、數字、特殊字符,暫且按拼音處理

參考:elasticsearch拼音插件安裝以及(IK+pinyin使用)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/74044.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/74044.shtml
英文地址,請注明出處:http://en.pswp.cn/web/74044.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

arm64位FFmpeg與X264庫

參考鏈接&#xff1a; https://blog.csdn.net/gitblog_09700/article/details/142945092

機器學習與深度學習4:數據集處理Dataset,DataLoader,batch_size

深度學習中&#xff0c;我們能看到別人的代碼中都有一個繼承Dataset類的數據集處理過程&#xff0c;這也是深度學習處理數據集的的基礎&#xff0c;下面介紹這個數據集的定義和使用&#xff1a; 1、數據集加載 1.1 通用的定義 Bach&#xff1a;表示每次喂給模型的數據 Epoc…

MySQL數據庫和表的操作之SQL語句

&#x1f3af; 本文專欄&#xff1a;MySQL深入淺出 &#x1f680; 作者主頁&#xff1a;小度愛學習 MySQL數據庫和表的操作 關系型數據庫&#xff0c;都是遵循SQL語法進行數據查詢和管理的。 SQL語句 什么是sql SQL&#xff1a;結構化查詢語言(Structured Query Language)&…

ubuntu開發mcu環境

# 編輯 vim或者vscode # 編譯 arm-none-eabi # 燒寫 openocd 若是默認安裝&#xff0c;會在/usr/share/openocd/scripts/{interface,target} 有配置接口和目標版配置 示例&#xff1a; openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg 啟動后&#xff0c;會…

Windows模仿Mac大小寫切換, 中英文切換

CapsLock 功能優化腳本部署指南 部署步驟 第一步&#xff1a;安裝 AutoHotkey v2 訪問 AutoHotkey v2 官網下載并安裝最新版本安裝時勾選 "Add Compile Script to context menus" 第二步&#xff1a;部署腳本 直接運行 (調試推薦) 新建文本文件&#xff0c;粘貼…

Selenium Web自動化如何快速又準確的定位元素路徑,強調一遍是元素路徑

如果文章對你有用&#xff0c;請給個贊&#xff01; 匹配的ChromeDriver和瀏覽器版本是更好完成自動化的基礎&#xff0c;可以從這里去下載驅動程序&#xff1a; 最全ChromeDriver下載含win linux mac 最新版本134.0.6998.165 持續更新..._chromedriver 134-CSDN博客 如果你問…

CSRF vs SSRF詳解

一、CSRF&#xff08;跨站請求偽造&#xff09;攻擊全解 攻擊原理示意圖 受害者瀏覽器 ├── 已登錄銀行網站&#xff08;Cookie存活&#xff09; └── 訪問惡意網站執行&#xff1a;<img src"http://bank.com/transfer?tohacker&amount1000000">核心…

Python PDF解析利器:pdfplumber | AI應用開發

Python PDF解析利器&#xff1a;pdfplumber全面指南 1. 簡介與安裝 1.1 pdfplumber概述 pdfplumber是一個Python庫&#xff0c;專門用于從PDF文件中提取文本、表格和其他信息。相比其他PDF處理庫&#xff0c;pdfplumber提供了更直觀的API和更精確的文本定位能力。 主要特點…

niuhe 插件教程 - 配置 MCP讓AI更聰明

niuhe 插件官方教程已經上線, 請訪問: http://niuhe.zuxing.net niuhe 連接 MCP 介紹 API 文檔的未來&#xff1a;MCP&#xff0c;讓協作像聊天一樣簡單. MCP 是 Model Context Protocol(模型上下文協議)的縮寫&#xff0c;是 2024 年 11 月 Claude 的公司 Anthropic 推出并開…

26考研——排序_插入排序(8)

408答疑 文章目錄 二、插入排序基本概念插入排序方法直接插入排序算法描述示例性能分析 折半插入排序改進點算法步驟性能分析 希爾排序相關概念示例分析希爾排序的效率效率分析空間復雜度時間復雜度 九、參考資料鮑魚科技課件26王道考研書 二、插入排序 基本概念 定義&#x…

精華貼分享|從不同的交易理論來理解頭肩形態,殊途同歸

本文來源于量化小論壇策略分享會板塊精華帖&#xff0c;作者為孫小迪&#xff0c;發布于2025年2月17日。 以下為精華帖正文&#xff1a; 01 前言 學習了一段時間交易后&#xff0c;我發現在幾百年的歷史中&#xff0c;不同門派的交易理論對同一種市場特征的稱呼不一樣&#x…

leetcode437.路徑總和|||

對于根結點來說&#xff0c;可以選擇當前結點為路徑也可以不選擇&#xff0c;但是一旦選擇當前結點為路徑那么后續都必須要選擇結點作為路徑&#xff0c;不然路徑不連續是不合法的&#xff0c;所以這里分開出來兩個方法進行遞歸 由于力扣最后一個用例解答錯誤&#xff0c;分析…

北斗導航 | 改進奇偶矢量法的接收機自主完好性監測算法原理,公式,應用,RAIM算法研究綜述,matlab代碼

改進奇偶矢量法的接收機自主完好性監測算法研究 摘要 接收機自主完好性監測(RAIM)是保障全球導航衛星系統(GNSS)安全性的核心技術。針對傳統奇偶矢量法在噪聲敏感性、多故障隔離能力上的缺陷,本文提出一種基于加權奇偶空間與動態閾值的改進算法。通過引入觀測值權重矩陣重…

深度神經網絡全解析:原理、結構與方法對比

深度神經網絡全解析&#xff1a;原理、結構與方法對比 1. 引言 隨著人工智能的發展&#xff0c;深度神經網絡&#xff08;Deep Neural Network&#xff0c;DNN&#xff09;已經成為圖像識別、自然語言處理、語音識別、自動駕駛等領域的核心技術。相比傳統機器學習方法&#x…

經典論文解讀系列:MapReduce 論文精讀總結:簡化大規模集群上的數據處理

&#x1f9e0; MapReduce 論文解讀總結&#xff1a;簡化大規模集群上的數據處理 原文標題&#xff1a;MapReduce: Simplified Data Processing on Large Clusters 作者&#xff1a;Jeffrey Dean & Sanjay Ghemawat 發表時間&#xff1a;2004 年 發表機構&#xff1a;Google…

通過Appium理解MCP架構

MCP即Model Context Protocol&#xff08;模型上下文協議&#xff09;&#xff0c;是由Anthropic公司于2024年11月26日推出的開放標準框架&#xff0c;旨在為大型語言模型與外部數據源、工具及系統建立標準化交互協議&#xff0c;以打破AI與數據之間的連接壁壘。 MCP架構與Appi…

網頁版五子棋項目的問題處理

文章目錄 config.WebSocketConfig將鍵值對加?OnlineUserManager中線程安全、鎖ObjectMapper來處理json針對多開情況的判定處理連接關閉、異常&#xff08;玩家中途退出&#xff09;后的不合理操作游戲大廳數據更新 config.WebSocketConfig 把MatchAPI注冊進去 ? 在addHandle…

【初探數據結構】歸并排序與計數排序的序曲

&#x1f4ac; 歡迎討論&#xff1a;在閱讀過程中有任何疑問&#xff0c;歡迎在評論區留言&#xff0c;我們一起交流學習&#xff01; &#x1f44d; 點贊、收藏與分享&#xff1a;如果你覺得這篇文章對你有幫助&#xff0c;記得點贊、收藏&#xff0c;并分享給更多對數據結構感…

算法刷題記錄——LeetCode篇(8.7) [第761~770題](持續更新)

更新時間&#xff1a;2025-03-30 算法題解目錄匯總&#xff1a;算法刷題記錄——題解目錄匯總技術博客總目錄&#xff1a;計算機技術系列博客——目錄頁 優先整理熱門100及面試150&#xff0c;不定期持續更新&#xff0c;歡迎關注&#xff01; 763. 劃分字母區間 給你一個字…

Pod 網絡與 CNI 的作用

在 Kubernetes 中&#xff0c;Pod 網絡 是實現容器間通信的核心機制&#xff0c;每個 Pod 擁有獨立的 IP 地址&#xff0c;可直接跨節點通信。CNI&#xff08;Container Network Interface&#xff09; 是 Kubernetes 的網絡插件標準&#xff0c;負責為 Pod 分配 IP、配置網絡規…