什麼是分詞器
分詞器就是把一個文檔切分成詞語,在分詞之後es才能建立反向索引
es内置了多種分詞器,但是如果要中文分詞,需要自己安裝中文的分詞器
IKAnalyzer是一個開源的,基于java語言開發的輕量級的中文分詞工具包。
安裝IK分詞器
可以使用elasticsearch的插件管理elasticsearch-plugin來自動安裝
下載下傳位址:https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.3.0/elasticsearch-analysis-ik-7.3.0.zip
執行安裝指令
/usr/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.3.0/elasticsearch-analysis-ik-7.3.0.zip
它會先下載下傳,然後彈框輸入y完成安裝
安裝完成後重新開機elasticsearch和kibana
IK分詞器的模式
- ik_max_word:細粒度拆分
- ik_smart:粗粒度的拆分
在kibana中測試
使用擴充詞典
擴充詞:就是不想讓哪些詞被分開,在分詞器分詞的時候讓他們分成一個詞。
自定義擴充詞庫
進入analysis-ik
cd /usr/elasticsearch/config/analysis-ik/
新增自定義擴充詞詞典,寫入:要有夢
vim test_ext_dict.dic
檢視一下
把自定義詞典加入IKAnalyzer.cfg.xml配置中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴充配置</comment>
<!--使用者可以在這裡配置自己的擴充字典 -->
<entry key="ext_dict">test_ext_dict.dic</entry>
<!--使用者可以在這裡配置自己的擴充停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--使用者可以在這裡配置遠端擴充字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--使用者可以在這裡配置遠端擴充停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
重新開機elasticsearch
在kibana中測試擴充詞
使用停用詞典
新增自定義停用詞詞典,寫入:要有
路徑:/usr/elasticsearch/config/analysis-ik/
vim test_stop_dict.dic
檢視一下
添加到IKAnalyzer.cfg.xml配置中
重新開機elasticsearch
<要有>這個詞已經沒有了
配置同義詞
路徑:/usr/elasticsearch/config/analysis-ik/
建立同義詞檔案synonym.txt
vim synonym.txt
寫入:china,中國
然後不做更改直接重新開機elasticsearch
進入kibana使用同義詞建立索引
PUT /testsynonym
{
"settings": {
"analysis": {
"filter": {
"word_sync": {
"type": "synonym",
"synonyms_path": "analysis-ik/synonym.txt"
}
},
"analyzer": {
"ik_sync_max_word": {
"filter": [
"word_sync"
],
"type": "custom",
"tokenizer": "ik_max_word"
},
"ik_sync_smart": {
"filter": [
"word_sync"
],
"type": "custom",
"tokenizer": "ik_smart"
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_sync_max_word",
"search_analyzer": "ik_sync_max_word"
}
}
}
}
然後插入一條資料
使用china搜尋