转自:http://keenwon.com/1404.html

为 Elasticsearch 添加中文分词,对比分词器效果

Posted in 后端 By KeenWon On 2014年12月12日 Views:
2,930

Elasticsearch中,内置了很多分词器(analyzers),例如standard (标准分词器)、english (英文分词)和chinese (中文分词)。其中standard 就是无脑的一个一个词(汉字)切分,所以适用范围广,但是精准度低;english 对英文更加智能,可以识别单数负数,大小写,过滤stopwords(例如“the”这个词)等;chinese
效果很差,后面会演示。这次主要玩这几个内容:安装中文分词ik,对比不同分词器的效果,得出一个较佳的配置。关于Elasticsearch,之前还写过两篇文章:Elasticsearch的安装,运行和基本配置 和 备份和恢复,需要的可以看下。

安装中文分词ik

Elasticsearch的中文分词很烂,所以我们需要安装ik。首先从 github上下载项目,解压:

  1. cd /tmp
  2. wget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip
  3. unzip master.zip
  4. cd elasticsearch-analysis-ik/

然后使用mvn package 命令,编译出jar包 elasticsearch-analysis-ik-1.4.0.jar。

  1. mvn package

将jar包复制到Elasticsearch的plugins/analysis-ik 目录下,再把解压出的ik目录(配置和词典等),复制到Elasticsearch的config 目录下。然后编辑配置文件elasticsearch.yml ,在后面加一行:

  1. index.analysis.analyzer.ik.type : "ik"

重启service elasticsearch restart 。搞定。

如果上面的mvn搞不定的话,你可以直接从 elasticsearch-rtf 项目中找到编译好的jar包和配置文件(我就是怎么干的)。

2014-12-14晚更新,今天是星期天,我在vps上安装ik分词,同样的步骤,总是提示MapperParsingException[Analyzer
[ik] not found for field [cn]],然后晚上跑到公司,发现我公司虚拟机上Elasticsearch的版本是1.3.2,vps上是1.3.4,猜是版本问题,直接把vps重新安装成最新的1.4.1,再安装ik,居然ok了……】

准备工作:创建索引,录入测试数据

先为后面的分词器效果对比做好准备,我的Elasticsearch部署在虚拟机 192.168.159.159:9200 上的,使用chrome的postman插件直接发http请求。第一步,创建index1 索引:

  1. PUT http://192.168.159.159:9200/index1
  2. {
  3. "settings": {
  4. "refresh_interval": "5s",
  5. "number_of_shards" : 1, // 一个主节点
  6. "number_of_replicas" : 0 // 0个副本,后面可以加
  7. },
  8. "mappings": {
  9. "_default_":{
  10. "_all": { "enabled": false } // 关闭_all字段,因为我们只搜索title字段
  11. },
  12. "resource": {
  13. "dynamic": false, // 关闭“动态修改索引”
  14. "properties": {
  15. "title": {
  16. "type": "string",
  17. "index": "analyzed",
  18. "fields": {
  19. "cn": {
  20. "type": "string",
  21. "analyzer": "ik"
  22. },
  23. "en": {
  24. "type": "string",
  25. "analyzer": "english"
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }

为了方便,这里的index1 索引,只有一个shards,没有副本。索引里只有一个叫resource 的type,只有一个字段title ,这就足够我们用了。title 本身使用标准分词器,title.cn 使用ik分词器,title.en 自带的英文分词器。然后是用bulk api批量添加数据进去:

  1. POST http://192.168.159.159:9200/_bulk
  2. { "create": { "_index": "index1", "_type": "resource", "_id": 1 } }
  3. { "title": "周星驰最新电影" }
  4. { "create": { "_index": "index1", "_type": "resource", "_id": 2 } }
  5. { "title": "周星驰最好看的新电影" }
  6. { "create": { "_index": "index1", "_type": "resource", "_id": 3 } }
  7. { "title": "周星驰最新电影,最好,新电影" }
  8. { "create": { "_index": "index1", "_type": "resource", "_id": 4 } }
  9. { "title": "最最最最好的新新新新电影" }
  10. { "create": { "_index": "index1", "_type": "resource", "_id": 5 } }
  11. { "title": "I'm not happy about the foxes" }

注意bulk api要“回车”换行,不然会报错。

各种比较

1、对比ik分词,chinese分词和standard分词

  1. POST http://192.168.159.159:9200/index1/_analyze?analyzer=ik
  2. 联想召回笔记本电源线

ik测试结果:

  1. {
  2. "tokens": [
  3. {
  4. "token": "联想",
  5. "start_offset": 0,
  6. "end_offset": 2,
  7. "type": "CN_WORD",
  8. "position": 1
  9. },
  10. {
  11. "token": "召回",
  12. "start_offset": 2,
  13. "end_offset": 4,
  14. "type": "CN_WORD",
  15. "position": 2
  16. },
  17. {
  18. "token": "笔记本",
  19. "start_offset": 4,
  20. "end_offset": 7,
  21. "type": "CN_WORD",
  22. "position": 3
  23. },
  24. {
  25. "token": "电源线",
  26. "start_offset": 7,
  27. "end_offset": 10,
  28. "type": "CN_WORD",
  29. "position": 4
  30. }
  31. ]
  32. }

自带chinese和standard分词器的结果:

  1. {
  2. "tokens": [
  3. {
  4. "token": "联",
  5. "start_offset": 0,
  6. "end_offset": 1,
  7. "type": "<IDEOGRAPHIC>",
  8. "position": 1
  9. },
  10. {
  11. "token": "想",
  12. "start_offset": 1,
  13. "end_offset": 2,
  14. "type": "<IDEOGRAPHIC>",
  15. "position": 2
  16. },
  17. {
  18. "token": "召",
  19. "start_offset": 2,
  20. "end_offset": 3,
  21. "type": "<IDEOGRAPHIC>",
  22. "position": 3
  23. },
  24. {
  25. "token": "回",
  26. "start_offset": 3,
  27. "end_offset": 4,
  28. "type": "<IDEOGRAPHIC>",
  29. "position": 4
  30. },
  31. {
  32. "token": "笔",
  33. "start_offset": 4,
  34. "end_offset": 5,
  35. "type": "<IDEOGRAPHIC>",
  36. "position": 5
  37. },
  38. {
  39. "token": "记",
  40. "start_offset": 5,
  41. "end_offset": 6,
  42. "type": "<IDEOGRAPHIC>",
  43. "position": 6
  44. },
  45. {
  46. "token": "本",
  47. "start_offset": 6,
  48. "end_offset": 7,
  49. "type": "<IDEOGRAPHIC>",
  50. "position": 7
  51. },
  52. {
  53. "token": "电",
  54. "start_offset": 7,
  55. "end_offset": 8,
  56. "type": "<IDEOGRAPHIC>",
  57. "position": 8
  58. },
  59. {
  60. "token": "源",
  61. "start_offset": 8,
  62. "end_offset": 9,
  63. "type": "<IDEOGRAPHIC>",
  64. "position": 9
  65. },
  66. {
  67. "token": "线",
  68. "start_offset": 9,
  69. "end_offset": 10,
  70. "type": "<IDEOGRAPHIC>",
  71. "position": 10
  72. }
  73. ]
  74. }

结论不必多说,对于中文,官方的分词器十分弱。

2、搜索关键词“最新”和“fox”

测试方法:

  1. POST http://192.168.159.159:9200/index1/resource/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "type": "most_fields",
  6. "query": "最新",
  7. "fields": [ "title", "title.cn", "title.en" ]
  8. }
  9. }
  10. }

我们修改queryfields 字段来对比。

1)搜索“最新”,字段限制在title.cn 的结果(只展示hit部分):

  1. "hits": [
  2. {
  3. "_index": "index1",
  4. "_type": "resource",
  5. "_id": "1",
  6. "_score": 1.0537746,
  7. "_source": {
  8. "title": "周星驰最新电影"
  9. }
  10. },
  11. {
  12. "_index": "index1",
  13. "_type": "resource",
  14. "_id": "3",
  15. "_score": 0.9057159,
  16. "_source": {
  17. "title": "周星驰最新电影,最好,新电影"
  18. }
  19. },
  20. {
  21. "_index": "index1",
  22. "_type": "resource",
  23. "_id": "4",
  24. "_score": 0.5319481,
  25. "_source": {
  26. "title": "最最最最好的新新新新电影"
  27. }
  28. },
  29. {
  30. "_index": "index1",
  31. "_type": "resource",
  32. "_id": "2",
  33. "_score": 0.33246756,
  34. "_source": {
  35. "title": "周星驰最好看的新电影"
  36. }
  37. }
  38. ]

再次搜索“最新”,字段限制在titletitle.en 的结果(只展示hit部分):

  1. "hits": [
  2. {
  3. "_index": "index1",
  4. "_type": "resource",
  5. "_id": "4",
  6. "_score": 1,
  7. "_source": {
  8. "title": "最最最最好的新新新新电影"
  9. }
  10. },
  11. {
  12. "_index": "index1",
  13. "_type": "resource",
  14. "_id": "1",
  15. "_score": 0.75,
  16. "_source": {
  17. "title": "周星驰最新电影"
  18. }
  19. },
  20. {
  21. "_index": "index1",
  22. "_type": "resource",
  23. "_id": "3",
  24. "_score": 0.70710677,
  25. "_source": {
  26. "title": "周星驰最新电影,最好,新电影"
  27. }
  28. },
  29. {
  30. "_index": "index1",
  31. "_type": "resource",
  32. "_id": "2",
  33. "_score": 0.625,
  34. "_source": {
  35. "title": "周星驰最好看的新电影"
  36. }
  37. }
  38. ]

结论:如果没有使用ik中文分词,会把“最新”当成两个独立的“字”,搜索准确性低。

2)搜索“fox”,字段限制在titletitle.cn ,结果为空,对于它们两个分词器,fox和foxes不同。再次搜索“fox”,字段限制在title.en ,结果如下:

  1. "hits": [
  2. {
  3. "_index": "index1",
  4. "_type": "resource",
  5. "_id": "5",
  6. "_score": 0.9581454,
  7. "_source": {
  8. "title": "I'm not happy about the foxes"
  9. }
  10. }
  11. ]

结论:中文和标准分词器,不对英文单词做任何处理(单复数等),查全率低。

我的最佳配置

其实最开始创建的索引已经是最佳配置了,在title 下增加cnen 两个fields,这样对中文,英文和其他什么乱七八糟文的效果都好点。就像前面说的,title 使用标准分词器,title.cn 使用ik分词器,title.en 使用自带的英文分词器,每次搜索同时覆盖。

-学习的比较浅,又不对的地方,欢迎留言-

标签:Elasticsearch ,经验

关于作者

KeenWon

原文链接:为Elasticsearch添加中文分词,对比分词器效果

@KeenWon 转载请保留原文链接

为 Elasticsearch 添加中文分词,对比分词器效果的更多相关文章

  1. 为Elasticsearch添加中文分词,对比分词器效果

    http://keenwon.com/1404.html Elasticsearch中,内置了很多分词器(analyzers),例如standard (标准分词器).english(英文分词)和chi ...

  2. 为Elasticsearch添加中文分词

    Elasticsearch的中文分词很烂,所以我们需要安装ik.首先从github上下载项目,解压: cd /tmp wget https://github.com/medcl/elasticsear ...

  3. elasticsearch教程--中文分词器作用和使用

    概述   本文都是基于elasticsearch安装教程 中的elasticsearch安装目录(/opt/environment/elasticsearch-6.4.0)为范例 环境准备 ·全新最小 ...

  4. 【自定义IK词典】Elasticsearch之中文分词器插件es-ik的自定义词库

    Elasticsearch之中文分词器插件es-ik 针对一些特殊的词语在分词的时候也需要能够识别 有人会问,那么,例如: 如果我想根据自己的本家姓氏来查询,如zhouls,姓氏“周”.      如 ...

  5. 如何给Elasticsearch安装中文分词器IK

    安装Elasticsearch安装中文分词器IK的步骤: 1. 停止elasticsearch 2.2的服务 2. 在以下地址下载对应的elasticsearch-analysis-ik插件安装包(版 ...

  6. Elasticsearch之中文分词器插件es-ik(博主推荐)

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...

  7. 沉淀再出发:ElasticSearch的中文分词器ik

    沉淀再出发:ElasticSearch的中文分词器ik 一.前言   为什么要在elasticsearch中要使用ik这样的中文分词呢,那是因为es提供的分词是英文分词,对于中文的分词就做的非常不好了 ...

  8. Elasticsearch之中文分词器

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch的中文分词器 1.单字分词 ...

  9. 在eclipse中构建solr项目+添加core+整合mysql+添加中文分词器

    最近在研究solr,这里只记录一下eclipse中构建solr项目,添加core,整合mysql,添加中文分词器的过程. 版本信息:solr版本6.2.0+tomcat8+jdk1.8 推荐阅读:so ...

随机推荐

  1. 搭建strom 的开发环境 - local mode

    Setting Up a Development Environment This page outlines what you need to do to get a Storm developme ...

  2. 64位BASM学习随笔(一)

     64位BASM学习随笔(一) Delphi的BASM一直是我最喜爱的内嵌汇编语言,同C/C++的内联汇编相比,它更方便,更具灵活性,由于C/C++的内联汇编仅仅能是或插入式的汇编代码,函数花括号 ...

  3. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

  4. Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards

    http://www.techrepublic.com/article/google-deepmind-ai-tries-it-hand-at-creating-hearthstone-magic-t ...

  5. FFT模板——copied from hzwer

    /* Welcome Hacking Wish You High Rating */ #include<iostream> #include<cstdio> #include& ...

  6. bzoj2120 数颜色——带修莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...

  7. CodeForces 680A&680B&680C&680D Round#356

    昨天晚上实在是=_=困...(浪了一天)就没有去打Codeforces 中午醒来看看题,还不太难. A题:模拟(水题 3minAC) // by Sirius_Ren #include <cst ...

  8. java热部署

    最近使用java做项目,研究了一下热部署,能够提高工作效率. 需要准备的工具: 1.安装文件http://update.zeroturnaround.com/update-site/ 2.破解 下载破 ...

  9. 5.20rieds切换数据库

  10. [hihocoder][Offer收割]编程练习赛50

    循环数组 计算a[i]的前缀和s[i],计算l[i]为1~i-1中最小的s值,r[i]为i~n中最大的s值. 则a[i]~a[n]满足性质的条件为r[i]-s[i-1]>0,a[1]~a[i-1 ...