当前位置:   article > 正文

ElasticSearch学习笔记(4)-IK中文分词器

ElasticSearch学习笔记(4)-IK中文分词器

一、分词器-介绍

IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包
•是一个基于Maven构建的项目
•具有60万字/秒的高速处理能力
•支持用户词典扩展定义
•下载地址: https://github.com/medcl/elasticsearch-analysis-ik/

二、ik分词器使用


IK分词器有两种分词模式:ik_max_word和ik_smart模式。


1、ik_max_word


会将文本做最细粒度的拆分,比如会将“乒乓球明年总冠军”拆分为“乒乓球、乒乓、球、明年、总冠军、
冠军。

  1. #方式一ik_max_word
  2. GET /_analyze
  3. {
  4.  "analyzer": "ik_max_word",
  5.  "text": "乒乓球明年总冠军"
  6. }

ik_max_word分词器执行如下

  1. {
  2.  "tokens" : [
  3.  {
  4.    "token" : "乒乓球",
  5.    "start_offset" : 0,
  6.    "end_offset" : 3,
  7.    "type" : "CN_WORD",
  8.    "position" : 0
  9.  },
  10.  {
  11.    "token" : "乒乓",
  12.    "start_offset" : 0,
  13.    "end_offset" : 2,
  14.    "type" : "CN_WORD",
  15.    "position" : 1
  16.  },
  17.  {
  18.    "token" : "球",
  19.    "start_offset" : 2,
  20.    "end_offset" : 3,
  21.    "type" : "CN_CHAR",
  22.    "position" : 2
  23.  },
  24.  {
  25.    "token" : "明年",
  26.    "start_offset" : 3,
  27.    "end_offset" : 5,
  28.    "type" : "CN_WORD",
  29.    "position" : 3
  30.  },
  31.  {
  32.    "token" : "总冠军",
  33.    "start_offset" : 5,
  34.    "end_offset" : 8,
  35.    "type" : "CN_WORD",
  36.    "position" : 4
  37.  },
  38.  {
  39.    "token" : "冠军",
  40. "start_offset" : 6,
  41.    "end_offset" : 8,
  42.    "type" : "CN_WORD",
  43.    "position" : 5
  44.  }
  45. ]
  46. }

2、ik_smart


会做最粗粒度的拆分,比如会将“乒乓球明年总冠军”拆分为乒乓球、明年、总冠军。

  1. #方式二ik_smart
  2. GET /_analyze
  3. {
  4.  "analyzer": "ik_smart",
  5.  "text": "乒乓球明年总冠军"
  6. }

ik_smart分词器执行如下:

  1. {
  2.  "tokens" : [
  3.  {
  4.    "token" : "乒乓球",
  5.    "start_offset" : 0,
  6.    "end_offset" : 3,
  7.    "type" : "CN_WORD",
  8.    "position" : 0
  9.  },
  10.  {
  11.    "token" : "明年",
  12.    "start_offset" : 3,
  13.    "end_offset" : 5,
  14.    "type" : "CN_WORD",
  15.    "position" : 1
  16.  },
  17.  {
  18.    "token" : "总冠军",
  19.    "start_offset" : 5,
  20.    "end_offset" : 8,
  21.    "type" : "CN_WORD",
  22.    "position" : 2
  23.  }
  24. ]
  25. }

由此可见 使用ik_smart可以将文本"text": "乒乓球明年总冠军"分成了【乒乓球】【明年】【总冠军】
这样看的话,这样的分词效果达到了我们的要求。

三、使用IK分词器-查询文档


•词条查询:term


词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时才匹配搜索


•全文查询:match


全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集。


1.创建索引,添加映射,并指定分词器为ik分词器

  1. PUT person2
  2. {
  3.  "mappings": {
  4.   "properties": {
  5.    "name": {
  6.     "type": "keyword"
  7.   },
  8.    "address": {
  9.     "type": "text",
  10.     "analyzer": "ik_max_word"
  11.   }
  12.  }
  13. }
  14. }

2.添加文档

POST /person2/_doc/1
{
"name":"张三",
"age":18,
"address":"北京市的海淀区"
}
POST /person2/_doc/2
{
"name":"李四",
"age":18,
"address":"北京市朝阳区"
}
POST /person2/_doc/3
{
"name":"王五",
"age":18,
"address":"北京市的¥¥¥¥昌平区"
}

3.查询映射

GET person2/_search

 4.查看分词效果

  1. GET _analyze
  2. {
  3. "analyzer": "ik_max_word",
  4. "text": "北京海淀"
  5. }

5.词条查询:term


查询person2中匹配到"北京"两字的词条

  1. GET /person2/_search
  2. {
  3.  "query": {
  4.   "term": {
  5.    "address": {
  6.     "value": "北京"
  7.   }
  8.  }
  9. }
  10. }

  1. GET /person2/_search
  2. {
  3. "query": {
  4. "term": {
  5. "address": {
  6. "value": "北京昌平" } } } }

这个查不到数据。

6.全文查询:match
全文查询会分析查询条件,先将查询条件进行分词,然后查询,求并集

  1. GET /person2/_search
  2. {
  3. "query": {
  4.  "match": {
  5.   "address":"北京昌平"
  6.  }
  7. }
  8. }

 

GET /person2/_search
{
"query": {
"match": {
"address":"北京 区"
}
}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/966574
推荐阅读
相关标签
  

闽ICP备14008679号