# ik安裝
1.https://github.com/medcl/elasticsearch-analysis-ik?
(參照自己es版本選擇ik版本)我選擇1.9.4版本(es:2.3.4),在頁面上直接下載 ,不要用git clone, clone下來是5以上版本
2.mvn clean package
?將target\relase\下zip包解壓到 es/plugins/ik/目錄下(沒有ik 創(chuàng)建即可 把解壓后文件直接放到ik下)
3.將詞典 即elasticsearch-analysis-ik/config下的文件 復(fù)制到es/config/ik下 (沒有ik創(chuàng)建)
4.配置(跳過也行),打開es/config/elasticsearch.yml文件,最后一行添加index.analysis.analyzer.default.type: ik,重啟elasticsearch
注釋:不用將jar 包c(diǎn)opy到 lib下
5.測試ik是否安裝成功
curl 'http://localhost:9200/_analyze?analyzer=ik_smart&pretty' -d '{ "text":"發(fā)展中國家" }'
curl 'http://localhost:9200/_analyze?analyzer=ik&pretty=true' -d '{ "text":"美國留給伊拉克的是個(gè)爛攤子嗎" }'
# 分詞插件使用:
1.ik 帶有兩個(gè)分詞器,根據(jù)需求選擇
ik_max_word :最細(xì)粒度的拆分,盡可能多的拆分出詞語
ik_smart:粗粒度的拆分,已被分出的詞語不會再次被拆分
示例:
#ik_smart
?curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_smart' -d '{ "text":"發(fā)展中國家" }'
分詞結(jié)果:發(fā)展中國家
#ik(ik_max_word)
curl 'localhost:9200/finance/_analyze?analyzer=ik&pretty' -d '{ "text":"發(fā)展中國家" }'
分詞結(jié)果:發(fā)展中國家、發(fā)展中、發(fā)展、發(fā)、展、中國、國家、家
1.創(chuàng)建索引 給索引指定分詞器
curl -XPUT 'http://localhost:9200/finance?pretty' -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"ik" : {
"tokenizer" : "ik_smart"
}
}
}
},
"mappings" : {
"toutiao" : {
"dynamic" : true,
"properties" : {
"title" : {
"type" : "string",
"analyzer" : "ik_smart"
}
}
}
}
}'
注釋:
創(chuàng)建finance 索引;type:toutiao;分詞字段:tit;analyzer :ik_smart
curl 'http://localhost:9200/finance/_analyze?analyzer=ik&pretty' -d '{ "text":"發(fā)展中國家" }'
curl 'http://localhost:9200/_analyze?analyzer=ik&pretty' -d '{ "text":"發(fā)展中國家" }'
給索引添加數(shù)據(jù)后,不能再指定使用哪種分詞
2.添加測試數(shù)據(jù),將mysql_laws_article.sh 腳本放到es/bin/下,執(zhí)行es目錄下 bin/mysql_laws_article.sh
3.測試查詢
curl -XPOST http://localhost:9200/finance/toutiao/_search?pretty? -d'
{
"query" : { "match" : { "title" : "股票" }},
"highlight" : {
"pre_tags" : [""],
"post_tags" : [""],
"fields" : {
"title" : {}
}
}
}
'