一、ES中文分詞插件(es-ik)
在中文數(shù)據(jù)檢索場景中,為了提供更好的檢索效果,需要在ES中集成中文分詞器,因為ES默認是按照英文的分詞規(guī)則進行分詞的,基本上可以認為是單字分詞,對中文分詞效果不理想。
ES之前是沒有提供中文分詞器的,現(xiàn)在官方也提供了一些,但是在中文分詞領(lǐng)域,IK分詞器是不可撼動的,所以在這里我們主要講一下如何在ES中集成IK這個中文分詞器。
1、下載
首先下載es-ik插件,需要到github上下載。
https://github.com/medcl/elasticsearch-analysis-ik
最終的下載地址為:
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.4/elasticsearch-analysis-ik-7.13.4.zip
百度網(wǎng)盤地址:
鏈接:https://pan.baidu.com/s/1KgBGetSvLpIUPtDz5fUM5Q?pwd=eh35
提取碼:eh35
注意:在ES中安裝IK插件的時候,需要在ES集群的所有節(jié)點中都安裝。
2、上傳
將下載好的elasticsearch-analysis-ik-7.13.4.zip上傳到bigdata01的/data/soft/ elasticsearch-7.13.4目錄中。
[root@bigdata01 elasticsearch-7.13.4]# ll elasticsearch-analysis-ik-7.13.4.zip
-rw-r--r--. 1 root root 4504502 Sep 3 2021 elasticsearch-analysis-ik-7.13.4.zip
3、遠程拷貝到其他節(jié)點
將elasticsearch-analysis-ik-7.13.4.zip遠程拷貝到bigdata02和bigdata03上。
[root@bigdata01 elasticsearch-7.13.4]# scp -rq elasticsearch-analysis-ik-7.13.4.zip bigdata02:/data/soft/elasticsearch-7.13.4
[root@bigdata01 elasticsearch-7.13.4]# scp -rq elasticsearch-analysis-ik-7.13.4.zip bigdata03:/data/soft/elasticsearch-7.13.4
4、安裝
在bigdata01節(jié)點離線安裝IK插件。
[root@bigdata01 elasticsearch-7.13.4]# bin/elasticsearch-plugin install file:///data/soft/elasticsearch-7.13.4/elasticsearch-analysis-ik-7.13.4.zip
注意:在安裝的過程中會有警告信息提示需要輸入y確認繼續(xù)向下執(zhí)行。
[=================================================] 100%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.
Continue with installation? [y/N]y
最后看到如下內(nèi)容就表示安裝成功了。
-> Installed analysis-ik
-> Please restart Elasticsearch to activate any plugins installed
注意:插件安裝成功之后在elasticsearch-7.13.4的config和plugins目錄下會產(chǎn)生一個analysis-ik目錄。
config目錄下面的analysis-ik里面存儲的是ik的配置文件信息。
[root@bigdata01 elasticsearch-7.13.4]# cd config/
[root@bigdata01 config]# ll analysis-ik/
total 8260
-rwxrwxrwx. 1 root root 5225922 Feb 27 20:57 extra_main.dic
-rwxrwxrwx. 1 root root 63188 Feb 27 20:57 extra_single_word.dic
-rwxrwxrwx. 1 root root 63188 Feb 27 20:57 extra_single_word_full.dic
-rwxrwxrwx. 1 root root 10855 Feb 27 20:57 extra_single_word_low_freq.dic
-rwxrwxrwx. 1 root root 156 Feb 27 20:57 extra_stopword.dic
-rwxrwxrwx. 1 root root 625 Feb 27 20:57 IKAnalyzer.cfg.xml
-rwxrwxrwx. 1 root root 3058510 Feb 27 20:57 main.dic
-rwxrwxrwx. 1 root root 123 Feb 27 20:57 preposition.dic
-rwxrwxrwx. 1 root root 1824 Feb 27 20:57 quantifier.dic
-rwxrwxrwx. 1 root root 164 Feb 27 20:57 stopword.dic
-rwxrwxrwx. 1 root root 192 Feb 27 20:57 suffix.dic
-rwxrwxrwx. 1 root root 752 Feb 27 20:57 surname.dic
plugins目錄下面的analysis-ik里面存儲的是ik的核心jar包。
[root@bigdata01 elasticsearch-7.13.4]# cd plugins/
[root@bigdata01 plugins]# ll analysis-ik/
total 1428
-rwxrwxrwx. 1 root root 263965 Feb 27 20:56 commons-codec-1.9.jar
-rwxrwxrwx. 1 root root 61829 Feb 27 20:56 commons-logging-1.2.jar
-rwxrwxrwx. 1 root root 54626 Feb 27 20:56 elasticsearch-analysis-ik-7.13.4.jar
-rwxrwxrwx. 1 root root 736658 Feb 27 20:56 httpclient-4.5.2.jar
-rwxrwxrwx. 1 root root 326724 Feb 27 20:56 httpcore-4.4.4.jar
-rwxrwxrwx. 1 root root 1807 Feb 27 20:56 plugin-descriptor.properties
-rwxrwxrwx. 1 root root 125 Feb 27 20:56 plugin-security.policy
5、在bigdata02節(jié)點離線安裝IK插件
[root@bigdata02 elasticsearch-7.13.4]# bin/elasticsearch-plugin install file:///data/soft/elasticsearch-7.13.4/elasticsearch-analysis-ik-7.13.4.zip
6、在bigdata03節(jié)點離線安裝IK插件
[root@bigdata03 elasticsearch-7.13.4]# bin/elasticsearch-plugin install file:///data/soft/elasticsearch-7.13.4/elasticsearch-analysis-ik-7.13.4.zip
7、如果集群正在運行,則需要停止集群
在bigdata01上停止。
[root@bigdata01 elasticsearch-7.13.4]# jps
1680 Elasticsearch
2047 Jps
[root@bigdata01 elasticsearch-7.13.4]# kill 1680
在bigdata02上停止。
[root@bigdata02 elasticsearch-7.13.4]# jps
1682 Elasticsearch
1866 Jps
[root@bigdata02 elasticsearch-7.13.4]# kill 1682
在bigdata03上停止。
[root@bigdata03 elasticsearch-7.13.4]# jps
1683 Elasticsearch
1803 Jps
[root@bigdata03 elasticsearch-7.13.4]# kill 1683
8、修改目錄權(quán)限
修改elasticsearch-7.13.4的plugins目錄下analysis-ik子目錄的權(quán)限
直接修改elasticsearch-7.13.4目錄的權(quán)限即可。
在bigdata01上執(zhí)行。
[root@bigdata01 elasticsearch-7.13.4]# cd ..
[root@bigdata01 soft]# chmod -R 777 elasticsearch-7.13.4
在bigdata02上執(zhí)行。
[root@bigdata02 elasticsearch-7.13.4]# cd ..
[root@bigdata02 soft]# chmod -R 777 elasticsearch-7.13.4
在bigdata03上執(zhí)行。
[root@bigdata03 elasticsearch-7.13.4]# cd ..
[root@bigdata03 soft]# chmod -R 777 elasticsearch-7.13.4
9、重新啟動ES集群
在bigdata01上執(zhí)行。
[root@bigdata01 soft]# su es
[es@bigdata01 soft]$ cd /data/soft/elasticsearch-7.13.4
[es@bigdata01 elasticsearch-7.13.4]$ bin/elasticsearch -d
在bigdata02上執(zhí)行。
[root@bigdata02 soft]# su es
[es@bigdata02 soft]$ cd /data/soft/elasticsearch-7.13.4
[es@bigdata02 elasticsearch-7.13.4]$ bin/elasticsearch -d
在bigdata03上執(zhí)行。
[root@bigdata03 soft]# su es
[es@bigdata03 soft]$ cd /data/soft/elasticsearch-7.13.4
[es@bigdata03 elasticsearch-7.13.4]$ bin/elasticsearch -d
10、驗證IK的分詞效果
首先使用默認分詞器測試中文分詞效果。
[root@bigdata01 soft]# curl -H "Content-Type: application/json" -XPOST 'http://bigdata01:9200/emp/_analyze?pretty' -d '{"text":"我們是中國人"}'
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "們",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "中",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "國",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "人",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
}
]
}
然后使用IK分詞器測試中文分詞效果。
[root@bigdata01 soft]# curl -H "Content-Type: application/json" -XPOST 'http://bigdata01:9200/emp/_analyze?pretty' -d '{"text":"我們是中國人","tokenizer":"ik_max_word"}'
{
"tokens" : [
{
"token" : "我們",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "中國人",
"start_offset" : 3,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "中國",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "國人",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 4
}
]
}
在這里我們發(fā)現(xiàn)分出來的單詞里面有一個 是,這個單詞其實可以認為是一個停用詞,在分詞的時候是不需要切分出來的。
在這被切分出來了,那也就意味著在進行停用詞過濾的時候沒有過濾掉。
針對ik這個詞庫而言,它的停用詞詞庫里面都有哪些單詞呢?
[root@bigdata01 elasticsearch-7.13.4]# cd config/analysis-ik/
[root@bigdata01 analysis-ik]# ll
total 8260
-rwxrwxrwx. 1 root root 5225922 Feb 27 20:57 extra_main.dic
-rwxrwxrwx. 1 root root 63188 Feb 27 20:57 extra_single_word.dic
-rwxrwxrwx. 1 root root 63188 Feb 27 20:57 extra_single_word_full.dic
-rwxrwxrwx. 1 root root 10855 Feb 27 20:57 extra_single_word_low_freq.dic
-rwxrwxrwx. 1 root root 156 Feb 27 20:57 extra_stopword.dic
-rwxrwxrwx. 1 root root 625 Feb 27 20:57 IKAnalyzer.cfg.xml
-rwxrwxrwx. 1 root root 3058510 Feb 27 20:57 main.dic
-rwxrwxrwx. 1 root root 123 Feb 27 20:57 preposition.dic
-rwxrwxrwx. 1 root root 1824 Feb 27 20:57 quantifier.dic
-rwxrwxrwx. 1 root root 164 Feb 27 20:57 stopword.dic
-rwxrwxrwx. 1 root root 192 Feb 27 20:57 suffix.dic
-rwxrwxrwx. 1 root root 752 Feb 27 20:57 surname.dic
[root@bigdata01 analysis-ik]# more stopword.dic
a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
ik的停用詞詞庫是stopword.dic這個文件,這個文件里面目前都是一些英文停用詞。
我們可以手工在這個文件中把中文停用詞添加進去,先添加 是 這個停用詞。
[root@bigdata01 analysis-ik]# vi stopword.dic
.....
是
然后把這個文件的改動同步到集群中的所有節(jié)點上。
[root@bigdata01 analysis-ik]# scp -rq stopword.dic bigdata02:/data/soft/elasticsearch-7.13.4/config/analysis-ik/
[root@bigdata01 analysis-ik]# scp -rq stopword.dic bigdata03:/data/soft/elasticsearch-7.13.4/config/analysis-ik/
重啟集群讓配置生效。
先停止bigdata01、bigdata02、bigdata03上的ES服務(wù)。
[root@bigdata01 analysis-ik]# jps
3051 Elasticsearch
3358 Jps
[root@bigdata01 analysis-ik]# kill 3051
[root@bigdata02 analysis-ik]$ jps
2496 Elasticsearch
2570 Jps
[root@bigdata02 analysis-ik]$ kill 2496
[root@bigdata03 analysis-ik]$ jps
2481 Jps
2412 Elasticsearch
[root@bigdata03 analysis-ik]$ kill 2412
啟動bigdata01、bigdata02、bigdata03上的ES服務(wù)。
[es@bigdata01 elasticsearch-7.13.4]$ bin/elasticsearch -d
[es@bigdata02 elasticsearch-7.13.4]$ bin/elasticsearch -d
[es@bigdata03 elasticsearch-7.13.4]$ bin/elasticsearch -d
再使用IK分詞器測試一下中文分詞效果。文章來源:http://www.zghlxwxcb.cn/news/detail-435155.html
[root@bigdata01 analysis-ik]# curl -H "Content-Type: application/json" -XPOST 'http://bigdata01:9200/test/_analyze?pretty' -d '{"text":"我們是中國人","tokenizer":"ik_max_word"}'
{
"tokens" : [
{
"token" : "我們",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "中國人",
"start_offset" : 3,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "中國",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "國人",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3
}
]
}
此時再查看會發(fā)現(xiàn)沒有"是" 這個單詞了,相當(dāng)于在過濾停用詞的時候把它過濾掉了。文章來源地址http://www.zghlxwxcb.cn/news/detail-435155.html
到了這里,關(guān)于Elasticsearch07:ES中文分詞插件(es-ik)安裝部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!