analyzer(分析器)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html

译文链接 : http://www.apache.wiki/pages/editpage.action?pageId=9405573

贡献者 : 程威ApacheCNApache中文网

[analyzed](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-index.html)(被分析)的 string fields(字符串字段)的值通过 analyzer(分析器)来传递,将字符串转换为一串 tokens(标记)标记或者 terms(词条)。例如,基于某种分析器,字符串 “The quick Brown Foxes“ 被解析为 : quickbrownfox这些是索引该字段的实际 terms(词条),可以用来有效地搜索大块文本内的单个单词。

这样的分析过程不仅发生在索引的时候,而且在查询时也需要 : 查询字符串需要通过相同(或类似的)analyzer分析器传递,以便尝试查找那些存在于索引的相同格式的 terms(词条)。

Elasticsearch 内置了许多 pre-defined analyzers(预定义的分析器),可以在不进一步配置的情况下使用。它还附带许多 character filters(字符过滤器),tokenizers(分词器)和Token Filters(标记过滤器)。可以用来组合配置每个索引的自定义analyzer(分析器)。

每一个查询,每一个字段或索引都可以指定分析器,在索引的时候,Elasticsearch 将按以下顺序查找 analyzer(分析器):

  • 定义在字段映射中的 analyzer(分析器)。
  • 索引设置中 default(默认)的 analyzer(分析器)。
  • standard(标准的)analyzer(分析器)。

在查询时,还有几层 :

  • full-text query(全文查找)中定义的 analyzer(分析器)。
  • 在字段映射中定义的 search_analyzer(搜索分析器)。
  • 在字段映射中定义的 analyzer(分析器)。
  • 在索引配置中 default_search(默认搜索的)analyzer(分析器)。
  • 索引设置中 default(默认)的 analyzer(分析器)。
  • standard(标准的)analyzer(分析器)。

为特定字段指定分析器的最简单的方法是在字段映射中进行定义,如下所示 :

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "text": { # 1
  7. "type": "text",
  8. "fields": {
  9. "english": { # 2
  10. "type": "text",
  11. "analyzer": "english"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }
  19. '
  20. curl -XGET 'localhost:9200/my_index/_analyze?pretty' -H 'Content-Type: application/json' -d' # 3
  21. {
  22. "field": "text",
  23. "text": "The quick Brown Foxes."
  24. }
  25. '
  26. curl -XGET 'localhost:9200/my_index/_analyze?pretty' -H 'Content-Type: application/json' -d' # 4
  27. {
  28. "field": "text.english",
  29. "text": "The quick Brown Foxes."
  30. }
  31. '

| 1 | **text** 字段使用默认的 standard(标准的)分析器。 | | 2 | text.english 多字段使用 english 分词器,可以删除 stop words(停用词)并应用于 stemming 词干。 | | 3 | 返回 tokens(标记): [thequickbrownfoxes]。 | | 4 | 返回 tokens(标记): [quickbrownfox]。 |

search_quote_analyzer(搜索引用分析器)

该 **search_quote_analyzer **设置允许你为短语指定 analyzer(分析器),这在处理禁用短语的 stop words(停用词)时特别有用。

要使用三个 analyzer(分析器)设置来禁用短语的停用词 :

  1. 一个 analyzer(分析器)设置成索引所有的 terms(词条)包括 stop words(停用词)。
  2. 一个 search_analyzer设置成将移除 stop words(停用词)的非短语查询。
  3. 一个 **search_quote_analyzer**设置不会移除 stop words(停用词)的短语查询。
  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "settings":{
  4. "analysis":{
  5. "analyzer":{
  6. "my_analyzer":{ # 1
  7. "type":"custom",
  8. "tokenizer":"standard",
  9. "filter":[
  10. "lowercase"
  11. ]
  12. },
  13. "my_stop_analyzer":{ # 2
  14. "type":"custom",
  15. "tokenizer":"standard",
  16. "filter":[
  17. "lowercase",
  18. "english_stop"
  19. ]
  20. }
  21. },
  22. "filter":{
  23. "english_stop":{
  24. "type":"stop",
  25. "stopwords":"_english_"
  26. }
  27. }
  28. }
  29. },
  30. "mappings":{
  31. "my_type":{
  32. "properties":{
  33. "title": {
  34. "type":"text",
  35. "analyzer":"my_analyzer", # 3
  36. "search_analyzer":"my_stop_analyzer", # 4
  37. "search_quote_analyzer":"my_analyzer" # 5
  38. }
  39. }
  40. }
  41. }
  42. }
  43. '
  1. PUT my_index/my_type/1
  2. {
  3. "title":"The Quick Brown Fox"
  4. }
  5. PUT my_index/my_type/2
  6. {
  7. "title":"A Quick Brown Fox"
  8. }
  9. GET my_index/my_type/_search
  10. {
  11. "query":{
  12. "query_string":{
  13. "query":"\"the quick brown fox\"" # 1
  14. }
  15. }
  16. }

| 1 | my_analyzer 分析器,用于标识所有 terms(词条)包括 stop words(停用词)。 | | 2 | 移除 **stop** **words**(停用词)的 my_stop_analyzer 分析器。 | | 3 | analyzer(分析器)设置指向将在索引时使用的 my_analyzer 分析器。 | | 4 | search_analyzer 设置指向 my_stop_analyzer,并移除非短语查询的 stop words(停用词)。 | | 5 | search_quote_analyzer 设置指向 my_analyzer 分析器,并确保 stop words(停用词)不会从短语查询中移除。 | | 1 | 由于查询时用括号括起来的,因此它被检测为短语查询。因此 search_quote_analyzer 会启动并确保停用词不会从查询中移除。my_analyzer 分析器将返回与其中一个文档相匹配的 terms(词条)[**the**,``**quick**,``**brown**,fox]。同时,将通过 my_stop_analyzer 分析器分析 terms(词条)查询,该分析器将过滤掉 stop words(停用词)。因此,搜索 The quick brown foxA quick brown fox 将返回两个文档,因为这两个文档都包含以下 tokens(词元)[**quick**,``**brown**,fox]。没有 search_quote_analyzer,将不可能对 phrase queries(短语查询)做到精确匹配,因为短语查询时 stop words(停用词)会被删除,从而导致两个文档都会被匹配到。 |