引言

上一篇主要讲解的是:linux下的kibana安装、windows下的kibana安装、使用Kibana操作ES。

本篇主要讲解的是:ik中文分词器,它提供了2个分词算法:ik_smart(最少切分)和ik_max_word(最细粒度划分)。

所谓的分词,就是把一段文本分成一个个的关键字,我们在搜索的时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个词,比如:"大飞哥大数据",就会被分为:"大","飞","哥","大","数","据",这显然是不符合要求的,所以我们需要单独去安装ik中文分词器,来解决这个问题。

ik中文分词器的安装

1、创建安装目录(所有节点)

[es@hadoop102 ~]$ mkdir -p /usr/local/elasticsearch/elasticsearch-7.8.0/plugins/ik    

2、上传安装包并解压(所有节点)

[root@hadoop102 ~]# yum install -y unzip    

[es@hadoop102 ~]$ unzip /tmp/elasticsearch-analysis-ik-7.8.0.zip -d /usr/local/elasticsearch/elasticsearch-7.8.0/plugins/ik

[root@hadoop102 ~]# chown -Rf es:es /usr/local/elasticsearch/    

3、关闭现有ES(所有节点)

[es@hadoop102 ~]# ps -ef|grep elastic

[es@hadoop102 ~]# kill -9 上一步查出的pid

4、启动ES(所有节点)

[root@hadoop102 ~]# su - es

[es@hadoop102 ]$ cd /usr/local/elasticsearch/elasticsearch-7.8.0

[es@hadoop102 ]$ bin/elasticsearch -d    

5、验证IK是否配置成功

[es@hadoop102 ~]$ cd /usr/local/elasticsearch/elasticsearch-7.8.0

[es@hadoop102 elasticsearch-7.8.0]$ bin/elasticsearch-plugin list

ik

ik中文分词器的实战

1、创建索引库

curl -X PUT 'http://192.168.8.102:9200/ik' -H "Content-Type:application/json" -d'{

"settings": {

    "number_of_shards": 5,

    "number_of_replicas": 1

  }

}'

2、设置mapping

ik中文分词器有2种分词算法:

ik_smart(最少切分):大飞哥大数据,分拆分成:大飞哥、大数据

ik_max_word(最细粒度划分):大飞哥大数据,分拆分成:大飞哥、飞哥、大飞、大、飞、哥、大数据、大数、数据、数、据,它会穷尽各种可能的组合。

实战操作:

curl -XPOST http://192.168.8.102:9200/ik/_mapping -H 'Content-Type:application/json' -d'{

"properties":{  

       "content":{

         "type":"text",

         "analyzer":"ik_max_word",

         "search_analyzer":"ik_smart"      

       }

     }

}'

解释说明:

curl -XPOST http://192.168.8.102:9200/ik/_mapping -H 'Content-Type:application/json' -d'{

"properties":{  

       "content":{

         "type":"text", #可以被分词

         "analyzer":"ik_max_word", #倒排索引,添加数据的时候分词,细粒度分词

         "search_analyzer":"ik_smart" #搜索时候的分词,精粒度分词      

       }

     }

}'

3、插入数据

curl -XPOST http://192.168.8.102:9200/ik/_create/1 -H 'Content-Type:application/json' -d'

{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'

curl -XPOST http://192.168.8.102:9200/ik/_create/2 -H 'Content-Type:application/json' -d'

{"content":"公安部:各地校车将享最高路权"}'

curl -XPOST http://192.168.8.102:9200/ik/_create/3 -H 'Content-Type:application/json' -d'

{"content":"美国留给伊拉克的是个烂摊子吗"}'

curl -XPOST http://192.168.8.102:9200/ik/_create/4 -H 'Content-Type:application/json' -d'

{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

4、查看数据

Elasticsearch掰开揉碎第7篇ik中文分词器_ik

5、测试分词

1、查找ik这个索引中内容包括:中国

curl -XGET http://192.168.8.102:9200/ik/_search?pretty -H 'Content-Type:application/json' -d'{

"query": {"term" : {"content" : "中国"}}

}'

2、返回的结果

{

 "took" : 12,

 "timed_out" : false,

 "_shards" : {

   "total" : 5,

   "successful" : 5,

   "skipped" : 0,

   "failed" : 0

 },

 "hits" : {

   "total" : {

     "value" : 2,

     "relation" : "eq"

   },

   "max_score" : 0.2876821,

   "hits" : [

     {

       "_index" : "ik",

       "_type" : "_doc",

       "_id" : "4",

       "_score" : 0.2876821,

       "_source" : {

         "content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"

       }

     },

     {

       "_index" : "ik",

       "_type" : "_doc",

       "_id" : "1",

       "_score" : 0.2876821,

       "_source" : {

         "content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"

       }

     }

   ]

 }

}

3、查找ik这个索引中内容包括:中

Elasticsearch掰开揉碎第7篇ik中文分词器_中文分词器_02

4、返回的结果

搜索结果为0个,这说明:”中” 并没有被分词,它不是一个单独的词,所以倒排索引时并没有把它规划成一个词。所以,要有个概念,不是说包含什么去查,而是有哪些分词可以供我们查询使用。

5、高亮查询

就是在指定内容,前后加了样式,控制指定内容显示效果

curl -XGET http://192.168.8.102:9200/ik/_search?pretty -H 'Content-Type:application/json' -d'

{

"query": {"match" : {"content" : "中国"}},

"highlight": {

"pre_tags": ["<font color=red>"],

 "post_tags": ["</font>"],

 "fields": {

     "content": {}

     }

   }  

}'

6、返回的结果

{

 "took" : 85,

 "timed_out" : false,

 "_shards" : {

   "total" : 5,

   "successful" : 5,

   "skipped" : 0,

   "failed" : 0

 },

 "hits" : {

   "total" : {

     "value" : 2,

     "relation" : "eq"

   },

   "max_score" : 0.2876821,

   "hits" : [

     {

       "_index" : "ik",

       "_type" : "_doc",

       "_id" : "4",

       "_score" : 0.2876821,

       "_source" : {

         "content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"

       },

       "highlight" : {

         "content" : [

           "<font color=red>中国</font>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"

         ]

       }

     },

     {

       "_index" : "ik",

       "_type" : "_doc",

       "_id" : "1",

       "_score" : 0.2876821,

       "_source" : {

         "content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"

       },

       "highlight" : {

         "content" : [

           "中韩渔警冲突调查:韩警平均每天扣1艘<font color=red>中国</font>渔船"

         ]

       }

     }

   ]

 }

}

6、Kibana平台上测试

1、ik_smart 最少切分

GET _analyze

{

   "analyzer": "ik_smart",

   "text": "中国共产党"

}

Elasticsearch掰开揉碎第7篇ik中文分词器_elasticsearch_03

2、ik_max_word 最细粒度划分

GET _analyze

{

   "analyzer": "ik_max_word",

   "text": "中国共产党"

}

Elasticsearch掰开揉碎第7篇ik中文分词器_ik_04

3、出现问题

GET _analyze

{

   "analyzer": "ik_smart",

   "text": "超级喜欢大飞哥bigdata"

}

GET _analyze

{

   "analyzer": "ik_max_word",

   "text": "超级喜欢大飞哥bigdata"

}

Elasticsearch掰开揉碎第7篇ik中文分词器_中文分词器_05

发现 大飞哥,被拆分开了,并不会当成一个完整的词。这种情况下,需要我们把自己需要的词,加入到分词器字典中。

4、自定义词加入分词器字典中(所有节点)

[es@hadoop102 ~]$ cd /usr/local/elasticsearch/elasticsearch-7.8.0/plugins/ik/config

[es@hadoop102 config]$ vi dafeige.dic      #加入

大飞哥

[es@hadoop102 config]$ vi IKAnalyzer.cfg.xml

Elasticsearch掰开揉碎第7篇ik中文分词器_analyzer_06

5、重启ES(所有节点)

[root@hadoop102 ~]# ps -ef|grep elastic

[root@hadoop102 ~]# kill -9 上一步查出的pid

[es@hadoop102 ]$ cd /usr/local/elasticsearch/elasticsearch-7.8.0

[es@hadoop102 ]$ bin/elasticsearch -d  

6、重新测试分词

GET _analyze

{

   "analyzer": "ik_smart",

   "text": "超级喜欢大飞哥bigdata"

}

GET _analyze

{

   "analyzer": "ik_max_word",

   "text": "超级喜欢大飞哥bigdata"

}

Elasticsearch掰开揉碎第7篇ik中文分词器_ik_07

可以看到结果正常了,大飞哥被识别成一个中文词汇,我们自定义的词汇成功了。

结束语

至此,Elasticsearch掰开揉碎系列的第7篇就结束了,本篇文章中主要要讲解的是:ik中文分词器的安装、ik中文分词器的实战。

下一篇我带来的是:logstash。后续的内容更精彩,敬请期待,感谢兄弟们的关注!!!