美文网首页
elasticsearch 分词器 ik

elasticsearch 分词器 ik

作者: Xlanglxx | 来源:发表于2017-08-17 12:41 被阅读83次

# 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 创建即可 把解压后文件直接放到ik下)

3.将词典 即elasticsearch-analysis-ik/config下的文件 复制到es/config/ik下 (没有ik创建)

4.配置(跳过也行),打开es/config/elasticsearch.yml文件,最后一行添加index.analysis.analyzer.default.type: ik,重启elasticsearch

注释:不用将jar 包copy到 lib下

5.测试ik是否安装成功

curl 'http://localhost:9200/_analyze?analyzer=ik_smart&pretty' -d '{ "text":"发展中国家" }'

curl 'http://localhost:9200/_analyze?analyzer=ik&pretty=true' -d '{ "text":"美国留给伊拉克的是个烂摊子吗" }'


# 分词插件使用:

1.ik 带有两个分词器,根据需求选择

ik_max_word :最细粒度的拆分,尽可能多的拆分出词语

ik_smart:粗粒度的拆分,已被分出的词语不会再次被拆分

示例:

#ik_smart

 curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_smart' -d '{ "text":"发展中国家" }'

分词结果:发展中国家

#ik(ik_max_word)

curl 'localhost:9200/finance/_analyze?analyzer=ik&pretty' -d '{ "text":"发展中国家" }'

分词结果:发展中国家、发展中、发展、发、展、中国、国家、家

1.创建索引 给索引指定分词器

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"

}

}

}

}

}'

注释:

创建finance 索引;type:toutiao;分词字段:tit;analyzer :ik_smart

curl 'http://localhost:9200/finance/_analyze?analyzer=ik&pretty' -d '{ "text":"发展中国家" }'

curl 'http://localhost:9200/_analyze?analyzer=ik&pretty' -d '{ "text":"发展中国家" }'

给索引添加数据后,不能再指定使用哪种分词

2.添加测试数据,将mysql_laws_article.sh 脚本放到es/bin/下,执行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" : {}

}

}

}

'

相关文章

网友评论

      本文标题:elasticsearch 分词器 ik

      本文链接:https://www.haomeiwen.com/subject/ueigrxtx.html