elasticsearch中文分词ik安装使用
2020-05-25 本文已影响0人
懒人程序猿
安装elasticsearch-analysis-ik
插件GitHub地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
根据elasticsearch版本查找对应的版本
# 进入elasticsearch安装目录的bin目录下,安装分词插件。
# 插件版本必须和elasticsearch版本保持一致。
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.0/elasticsearch-analysis-ik-6.8.0.zip
# 根据提示安装即可,中间需要确认。输入(y)即可
安装完成后重启elasticsearch,下面对比默认分词器和ik的区别和ik使用。
默认 standard
POST /_analyze
{
"text": "中国的改革开放",
"analyzer": "standard"
}

ik_smart(最粗粒度的拆分)
POST /_analyze
{
"text": "中国的改革开放",
"analyzer": "ik_smart"
}

ik_max_word(文本做最细粒度的拆分)
POST /_analyze
{
"text": "中国的改革开放",
"analyzer": "ik_max_word"
}

添加索引测试,创建一个名字为demo的索引1个主分片和一个副本分片。
映射doc类型,title和content字段使用ik_max_word分词。
PUT /demo
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
添加文档
POST /demo/doc/_bulk
{"index": {}}
{"content": "中国的改革开放,迎来了新的变化和发展。", "title": "新中国成立后的改变有哪些"}
{"index": {}}
{"content": "中国的发展促进了国际合作和世界经济的良好发展", "title": "世界经济体中,中国的位置是什么"}
{"index": {}}
{"content": "我国在国际地位中,处于上升阶段。不断提高国际地位", "title": "亚洲和世界欧美,中国在里面的位置很重要"}
高亮搜索
GET /demo/doc/_search
{
"query" : {
"match_phrase" : {
"content" : "世界经济"
}
},
"highlight" : {
"pre_tags" : ["<font color='red'>"],
"post_tags" : ["</font>"],
"fields" : {
"content" : {}
}
}
}
