ElasticSearch學習筆記之三十三 IK分詞器擴充套件字典及text全文型別資料分詞聚合查詢
阿新 • • 發佈:2018-12-11
ElasticSearch學習筆記之三十三 IK分詞器擴充套件字典及text全文型別資料分詞聚合查詢
專屬詞彙分詞失敗
前面我們已經知道了IK分詞器已經可以很好的為中文的text全文型別資料分詞,但是有一些特定行業的特定專屬詞彙,IK分詞器卻不能按照我們的設想來分詞,例如 人名/書名/專屬詞彙 等等
例如我們分析一下
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text": ["鬥破蒼穹真好看"]
}
結果如下
{
"tokens": [
{
"token": "鬥",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "破",
"start_offset": 1,
"end_offset": 2,
"type" : "CN_CHAR",
"position": 1
},
{
"token": "蒼穹",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 2
},
{
"token": "真",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 3
},
{
"token" : "好看",
"start_offset": 5,
"end_offset": 7,
"type": "CN_WORD",
"position": 4
}
]
}
很顯然,按照我們的設想,我們希望的分詞結果是鬥破蒼穹
,真
,好看
,但是我們卻沒有得到我們想要的結果。這個時候擴充套件字典就可以派上用場了。
擴充套件字典
檢視當前詞庫
# ls -l /usr/local/elasticsearch/config/analysis-ik/
總用量 8260
-rw-rw----. 1 elk elk 5225922 10月 16 10:49 extra_main.dic
-rw-rw----. 1 elk elk 63188 10月 16 10:49 extra_single_word.dic
-rw-rw----. 1 elk elk 63188 10月 16 10:49 extra_single_word_full.dic
-rw-rw----. 1 elk elk 10855 10月 16 10:49 extra_single_word_low_freq.dic
-rw-rw----. 1 elk elk 156 10月 16 10:49 extra_stopword.dic
-rw-rw----. 1 elk elk 644 12月 4 13:15 IKAnalyzer.cfg.xml
-rw-rw----. 1 elk elk 3058510 10月 16 10:49 main.dic
-rw-rw----. 1 elk elk 123 10月 16 10:49 preposition.dic
-rw-rw----. 1 elk elk 1824 10月 16 10:49 quantifier.dic
-rw-rw----. 1 elk elk 164 10月 16 10:49 stopword.dic
-rw-rw----. 1 elk elk 192 10月 16 10:49 suffix.dic
-rw-rw----. 1 elk elk 752 10月 16 10:49 surname.dic
自定義詞典
mkdir /usr/local/elasticsearch/config/analysis-ik/custom && /usr/local/elasticsearch/config/analysis-ik/custom
vim new_word.dic
將鬥破蒼穹
寫入到new_word.dic
更新配置
vim /usr/local/elasticsearch/config/analysis-ik/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">custom/new_word.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就可以了。
再次檢視分詞
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text": ["鬥破蒼穹真好看"]
}
結果如下:
{
"tokens": [
{
"token": "鬥破蒼穹",
"start_offset": 0,
"end_offset": 4,
"type": "CN_WORD",
"position": 0
},
{
"token": "真",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 1
},
{
"token": "好看",
"start_offset": 5,
"end_offset": 7,
"type": "CN_WORD",
"position": 2
}
]
}
text全文型別資料分詞聚合
當我們使用elasticsearch對text全文型別資料進行索引詞聚合的時候,elasticsearch會自動進行分詞將分詞後的結果進行聚合。獲取每一個分詞出現在文件的文件個數。
新建索引
PUT message
{
"mappings": {
"message": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart",
"fielddata": true
},
"content": {
"type": "text",
"analyzer": "ik_smart",
"fielddata": true
}
}
}
}
}
插入資料
PUT /message/message/1
{
"title":"鬥破蒼穹好看",
"content":"鬥破蒼穹真好看,就是更新慢 "
}
PUT /message/message/2
{
"title":"鬥破蒼穹沒看過",
"content":"鬥破蒼穹聽說好看沒看過"
}
PUT /message/message/3
{
"title":"鬥破蒼穹是啥",
"content":"不知道沒看過"
}
PUT /message/message/4
{
"title":"鬥破蒼穹是啥",
"content":"鬥破蒼穹是小說"
}
聚合查詢
GET message/message/_search
{
"aggs": {
"term_content": {
"terms": {
"field": "content",
"size": 20
}
}
},
"size": 0
}
結果如下:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"term_content": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "鬥破蒼穹",
"doc_count": 3
},
{
"key": "好看",
"doc_count": 2
},
{
"key": "沒",
"doc_count": 2
},
{
"key": "看過",
"doc_count": 2
},
{
"key": "不知道",
"doc_count": 1
},
{
"key": "聽說",
"doc_count": 1
},
{
"key": "小說",
"doc_count": 1
},
{
"key": "就是",
"doc_count": 1
},
{
"key": "慢",
"doc_count": 1
},
{
"key": "是",
"doc_count": 1
},
{
"key": "更新",
"doc_count": 1
},
{
"key": "真",
"doc_count": 1
}
]
}
}
}