ES添加elasticsearch-analysis-ik分词器

1、下载分词器包

https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v5.4.3

2、将分词器解压并放入plugins目录下。(一定要在plugins下面创建一个ik文件,然后将解压的elasticsearch-analysis-ik文件放入ik文件夹内

 3、重启es

kill `ps -ef | grep Elasticsearch | grep -v grep | awk '{print $2}'`   #kill es进程

/home/yangwj/elasticsearch-5.4.3/bin/elasticsearch -d #后台运行

4、创建一个索引

#创建索引名字叫news
curl -XPUT http://hadoop100:9200/news

5、为内容创建schema信息,schema信息指定了字段的分词器

#创建mapping(相当于数据中的schema信息,表名和字段名以及字段的类型)
curl -XPOST http://hadoop100:9200/news/fulltext/_mapping -d'
{
        "properties": {
            "content": {  #数据字段名称
                "type": "text", #content字典是text类型
                "analyzer": "ik_max_word", #分析器选择
                "search_analyzer": "ik_max_word"  # 搜索选择
            }
        }
    
}'

6、测试ik_max_word 和ik_smart 分词效果。(推荐ik_smart)

curl -XGET 'http://hadoop100:9200/_analyze?pretty&analyzer=ik_max_word' -d '联想是全球最大的笔记本厂商'

curl -XGET 'https://hadoop100:9200/_analyze?pretty&analyzer=ik_smart' -d '联想是全球最大的笔记本厂商'

7、案例测试

###添加内容,注意是content字段,我们添加schema也只是针对这个字段
curl -XPOST http://hadoop100:9200/news/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}'

curl -XPOST http://hadoop100:9200/news/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}'

curl -XPOST http://hadoop100:9200/news/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'

curl -XPOST http://hadoop100:9200/news/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'


###根据content字段内容搜索 
curl -XPOST http://hadoop100:9200/news/fulltext/_search  -d'
{
    "query" : { "match" : { "content" : "中国" }}, #match表示匹配,即content字段内容中有中国的分词就返回
    "highlight" : {
        "pre_tags" : ["<font color='red'>", "<tag2>"],
        "post_tags" : ["</font>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}'

8、schema信息扩展

curl -XPUT 'https://hadoop100:9200/iktest?pretty' -d '{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "ik" : {
                    "tokenizer" : "ik_max_word"
                }
            }
        }
    },
    "mappings" : {
        "article" : {
            "dynamic" : true,
            "properties" : {
                "subject" : {
                    "type" : "string",
                    "analyzer" : "ik_max_word"
                }
            }
        }
    }
}'

curl -XPUT 'https://hadoop100:9200/iktest?pretty' -d '{
    "settings" : {
        "analysis" : {
            "analyzer" : {
                "ik" : {
                    "tokenizer" : "ik_max_word"
                }
            }
        }
    },
    "mappings" : {
        "article" : {
            "dynamic" : true,
            "properties" : {
                "subject" : {
                    "type" : "string",
                    "analyzer" : "ik_max_word"
                }
            }
        }
    }
}'
原文地址:https://www.cnblogs.com/ywjfx/p/13466587.html