search_analyzer (搜索分析器)

通常情况下,我们在搜索和创建索引时使用的是同一分析器,以确保我们搜索是的词根与倒排索引中的词根拥有相同的格式。

但是有时我们又会有意识的在搜索时使用不同的分析器,例如使用 edge_ngram 解析器自动解析。

默认情况下,查询将会使用字段映射时定义的分析器,但也能通过 search_analyzer 设置来进行修改:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "autocomplete_filter": { #1
  7. "type": "edge_ngram",
  8. "min_gram": 1,
  9. "max_gram": 20
  10. }
  11. },
  12. "analyzer": {
  13. "autocomplete": {
  14. "type": "custom",
  15. "tokenizer": "standard",
  16. "filter": [
  17. "lowercase",
  18. "autocomplete_filter"
  19. ]
  20. }
  21. }
  22. }
  23. },
  24. "mappings": {
  25. "my_type": {
  26. "properties": {
  27. "text": {
  28. "type": "text",
  29. "analyzer": "autocomplete", #2
  30. "search_analyzer": "standard" #3
  31. }
  32. }
  33. }
  34. }
  35. }
  36. '
  37. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  38. {
  39. "text": "Quick Brown Fox" #4
  40. }
  41. '
  42. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  43. {
  44. "query": {
  45. "match": {
  46. "text": {
  47. "query": "Quick Br", #5
  48. "operator": "and"
  49. }
  50. }
  51. }
  52. }
  53. '

| 1 | Analysis 设置为传统的 autocomplete 分析器 | | 2 3 | text 字段使用 autocomplete 分析器进行索引,但是使用 standard 分析器进行搜索。 | | 4 | 这个字段将使用以下词根进行索引:[ q, qu, qui, quic, quick, b, br, bro, brow, brown, f, fo, fox ] | | 5 | 查询搜索将同时使用两个词根:[ quick, br ] |

可以通过查看 Index time search-as-you- type 获得此例的完整解释。

注意

同一索引相同名字的字段 search_analyzer 设置必须相同。他的值可以通过 PUT mapping API 进行覆盖修改。

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-analyzer.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10027256(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 郭峰ApacheCNApache中文网