分词器使用的两个情形:
1,Index time analysis. 创建或者更新文档时,会对文档进行分词
2,Search time analysis. 查询时,对查询语句分词

一、指定查询时使用哪个分词器的方式有:

1、查询时通过analyzer指定分词器

  1. GET test_index/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": {
  6. "query": "lin",
  7. "analyzer": "standard"
  8. }
  9. }
  10. }
  11. }

2、创建index mapping时指定search_analyzer

  1. #创建索引时,如果不指定分词时,会使用默认的standard
  2. PUT test_index
  3. {
  4. "mappings": {
  5. "doc": {
  6. "properties": {
  7. "title":{
  8. "type": "text",
  9. "analyzer": "whitespace", #指定写入时的分词器,es内置有多种analyzer
  10. "search_analyzer": "standard" #指定搜索时的分词器
  11. }
  12. }
  13. }
  14. }
  15. }

二、分词器API

1、查看分词结果

  1. POST _analyze
  2. {
  3. "analyzer": "standard",
  4. "text": "我是中国人"
  5. }

2、指定索引的自动查看分词结果

  1. POST /indexName/_analyze
  2. {
  3. "analyzer": "standard",
  4. "field":"hobby", #指定具体的字段名称
  5. "text": "我是中国人"
  6. }

3、修改分词器的设定

  1. #启用english停用词token filter,默认停词器是禁用的
  2. PUT /my_index
  3. {
  4. "settings": {
  5. "analysis": {
  6. "analyzer": {
  7. "es_std": { #分词器名称,
  8. "type": "standard",
  9. "stopwords": "_english_"
  10. }
  11. }
  12. }
  13. }
  14. }

三、ES分词器组成

分词器的组成:
1、character filter:在一段文本进行分词之前,先进行预处理,比如说最常见的就是,过滤html标签(hello —> hello),& —> and(I&you —> I and you)
2、tokenizer:按照一定的规则进行分词,hello you and me —> hello, you, and, me
3、token filter:对tokenizer输出的词进行增加(添加近义词)、修改、删除,如lowercase,stop word,synonymom,dogs —> dog,liked —> like,Tom —> tom,a/the/an —> 干掉,mother —> mom,small —> little
stop word 停用词: 了 的 呢。
一个分词器,很重要,将一段文本进行各种处理,最后处理好的结果才会拿去建立倒排索引。

内置的分词器:
1、standard 由以下组成

  • tokenizer:Standard Tokenizer
  • token filter:Standard Token Filter,Lower Case Token Filter,Stop Token Filter

2、whitespace 空格为分隔符

3、simple

4、stop 默认stopwords用english

5、keyword 不分词的

6、language 以特定的语言进行分词,如english

四、第三方分词器

es内置很多分词器,但是对中文分词并不友好,例如使用standard分词器对一句中文话进行分词,会分成一个字一个字的。这时可以使用第三方的Analyzer插件,比如 ik、jieba、pinyin等。
安装只需要把对应的插件下载解压到es的plugins目录下即可再重启生效。

如ik分词器的效果如下:

  1. GET _analyze
  2. {
  3. "analyzer": "ik_max_word", #ik分词器的名称
  4. "text": "你好吗?我有一句话要对你说呀。"
  5. }
  6. 结果:
  7. {
  8. "tokens": [
  9. {
  10. "token": "你好",
  11. "start_offset": 0,
  12. "end_offset": 2,
  13. "type": "CN_WORD",
  14. "position": 0
  15. },
  16. {
  17. "token": "好吗",
  18. "start_offset": 1,
  19. "end_offset": 3,
  20. "type": "CN_WORD",
  21. "position": 1
  22. },
  23. {
  24. "token": "我",
  25. "start_offset": 4,
  26. "end_offset": 5,
  27. "type": "CN_CHAR",
  28. "position": 2
  29. },
  30. {
  31. "token": "有",
  32. "start_offset": 5,
  33. "end_offset": 6,
  34. "type": "CN_CHAR",
  35. "position": 3
  36. },
  37. {
  38. "token": "一句话",
  39. "start_offset": 6,
  40. "end_offset": 9,
  41. "type": "CN_WORD",
  42. "position": 4
  43. },
  44. {
  45. "token": "一句",
  46. "start_offset": 6,
  47. "end_offset": 8,
  48. "type": "CN_WORD",
  49. "position": 5
  50. },
  51. {
  52. "token": "一",
  53. "start_offset": 6,
  54. "end_offset": 7,
  55. "type": "TYPE_CNUM",
  56. "position": 6
  57. },
  58. {
  59. "token": "句话",
  60. "start_offset": 7,
  61. "end_offset": 9,
  62. "type": "CN_WORD",
  63. "position": 7
  64. },
  65. {
  66. "token": "句",
  67. "start_offset": 7,
  68. "end_offset": 8,
  69. "type": "COUNT",
  70. "position": 8
  71. },
  72. {
  73. "token": "话",
  74. "start_offset": 8,
  75. "end_offset": 9,
  76. "type": "CN_CHAR",
  77. "position": 9
  78. },
  79. {
  80. "token": "要对",
  81. "start_offset": 9,
  82. "end_offset": 11,
  83. "type": "CN_WORD",
  84. "position": 10
  85. },
  86. {
  87. "token": "你",
  88. "start_offset": 11,
  89. "end_offset": 12,
  90. "type": "CN_CHAR",
  91. "position": 11
  92. },
  93. {
  94. "token": "说呀",
  95. "start_offset": 12,
  96. "end_offset": 14,
  97. "type": "CN_WORD",
  98. "position": 12
  99. }
  100. ]
  101. }

五、自定义分词器

  1. PUT /my_index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "char_filter": { #定义分词第一步的filter,预处理
  6. "&_to_and": {
  7. "type": "mapping",
  8. "mappings": ["&=> and"] # 把&符号转换为and字符
  9. }
  10. },
  11. "filter": { # 定义第三步的filter,过滤器把thea的字符去除
  12. "my_stopwords": {
  13. "type": "stop",
  14. "stopwords": ["the", "a"]
  15. }
  16. },
  17. "analyzer": {
  18. "my_analyzer": { #第二步分词器
  19. "type": "custom",
  20. "char_filter": ["html_strip", "&_to_and"], #使用哪些预处理器,有一个es自带的和一个自定义的
  21. "tokenizer": "standard", # 使用默认的分词策略
  22. "filter": ["lowercase", "my_stopwords"] # 第三步,使用一个默认的和自定义的filter
  23. }
  24. }
  25. }
  26. }
  27. }