fields(字段)

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

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

贡献者 : 郭峰ApacheCNApache中文网

fields(字段)

我们经常会因为不同的目的将同一个字段用不同的方式索引。这就相当于实现了 multi-fields。例如,一个 string 类型字段可以被映射成 text 字段作为 full-text 进行搜索,同时也可以作为 keyword 字段用于排序和聚合:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "city": {
  7. "type": "text",
  8. "fields": {
  9. "raw": { #1
  10. "type": "keyword"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. '
  19. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  20. {
  21. "city": "New York"
  22. }
  23. '
  24. curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
  25. {
  26. "city": "York"
  27. }
  28. '
  29. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  30. {
  31. "query": {
  32. "match": {
  33. "city": "york" #2
  34. }
  35. },
  36. "sort": {
  37. "city.raw": "asc" #3
  38. },
  39. "aggs": {
  40. "Cities": {
  41. "terms": {
  42. "field": "city.raw" #4
  43. }
  44. }
  45. }
  46. }
  47. '

| 1 | city.raw 字段是 city 字段的 keyword 类型字段 | | 2 | city 字段将被当做 full text 进行搜索 | | 3 4 | city.raw 可用于排序和聚合 |

备注

Multi_fields 不会改变原始的 _source 字段。

注意

同一索引相同字段名可以设置不同的 fields。可以通过PUT mapping API 在已经存在的字段加入新的 multi-fields

Multi-fields with multiple analyzers(多分析器处理多字段)

multi-fields 的另一种使用情况是同一字段使用不同的解析方式,使其能更好的检索。例如,我们可以用标准分析器对字段进行索引,它将文本分解为单词,再用英文分析器将单词分成词根:

  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 -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  21. { "text": "quick brown fox" } #3
  22. '
  23. curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
  24. { "text": "quick brown foxes" } #4
  25. '
  26. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  27. {
  28. "query": {
  29. "multi_match": {
  30. "query": "quick brown foxes",
  31. "fields": [ #5
  32. "text",
  33. "text.english"
  34. ],
  35. "type": "most_fields" #6
  36. }
  37. }
  38. }
  39. '

| 1 | text 字段使用标准分析器。 | | 2 | text.english 字段使用英文分析器。 | | 3 4 | 同时索引两个文档,一个使用 fox,另一个使用 foxes。 | | 5 6 | 同时搜索 texttext.english 字段,并合并其评分。 |

text 字段在第一个文档中包含词根 fox,在第二个文档中包含词根 foxestext.english 字段在两个文档同时包含词根 fox,因为 foxes fox 的衍生词。

字符串搜索会为 text 字段使用标准分析器解析,为 text.english 字段使用英文分析器解析。衍生字段将会使搜索 foxes 的同时匹配到 fox。这使我们能尽可能多的匹配到文档。同时,搜索没有衍生的 text 字段时,我们会在文档精确匹配 foxes 的时候提高其检索评分。