multi-fields.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. [[multi-fields]]
  2. === `fields`
  3. It is often useful to index the same field in different ways for different
  4. purposes. This is the purpose of _multi-fields_. For instance, a `string`
  5. field could be mapped as a `text` field for full-text
  6. search, and as a `keyword` field for sorting or aggregations:
  7. [source,js]
  8. --------------------------------------------------
  9. PUT my_index
  10. {
  11. "mappings": {
  12. "my_type": {
  13. "properties": {
  14. "city": {
  15. "type": "text",
  16. "fields": {
  17. "raw": { <1>
  18. "type": "keyword"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. PUT my_index/my_type/1
  27. {
  28. "city": "New York"
  29. }
  30. PUT my_index/my_type/2
  31. {
  32. "city": "York"
  33. }
  34. GET my_index/_search
  35. {
  36. "query": {
  37. "match": {
  38. "city": "york" <2>
  39. }
  40. },
  41. "sort": {
  42. "city.raw": "asc" <3>
  43. },
  44. "aggs": {
  45. "Cities": {
  46. "terms": {
  47. "field": "city.raw" <3>
  48. }
  49. }
  50. }
  51. }
  52. --------------------------------------------------
  53. // AUTOSENSE
  54. <1> The `city.raw` field is a `keyword` version of the `city` field.
  55. <2> The `city` field can be used for full text search.
  56. <3> The `city.raw` field can be used for sorting and aggregations
  57. NOTE: Multi-fields do not change the original `_source` field.
  58. TIP: The `fields` setting is allowed to have different settings for fields of
  59. the same name in the same index. New multi-fields can be added to existing
  60. fields using the <<indices-put-mapping,PUT mapping API>>.
  61. ==== Multi-fields with multiple analyzers
  62. Another use case of multi-fields is to analyze the same field in different
  63. ways for better relevance. For instance we could index a field with the
  64. <<analysis-standard-analyzer,`standard` analyzer>> which breaks text up into
  65. words, and again with the <<english-analyzer,`english` analyzer>>
  66. which stems words into their root form:
  67. [source,js]
  68. --------------------------------------------------
  69. PUT my_index
  70. {
  71. "mappings": {
  72. "my_type": {
  73. "properties": {
  74. "text": { <1>
  75. "type": "text",
  76. "fields": {
  77. "english": { <2>
  78. "type": "text",
  79. "analyzer": "english"
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. PUT my_index/my_type/1
  88. { "text": "quick brown fox" } <3>
  89. PUT my_index/my_type/2
  90. { "text": "quick brown foxes" } <3>
  91. GET my_index/_search
  92. {
  93. "query": {
  94. "multi_match": {
  95. "query": "quick brown foxes",
  96. "fields": [ <4>
  97. "text",
  98. "text.english"
  99. ],
  100. "type": "most_fields" <4>
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. // AUTOSENSE
  106. <1> The `text` field uses the `standard` analyzer.
  107. <2> The `text.english` field uses the `english` analyzer.
  108. <3> Index two documents, one with `fox` and the other with `foxes`.
  109. <4> Query both the `text` and `text.english` fields and combine the scores.
  110. The `text` field contains the term `fox` in the first document and `foxes` in
  111. the second document. The `text.english` field contains `fox` for both
  112. documents, because `foxes` is stemmed to `fox`.
  113. The query string is also analyzed by the `standard` analyzer for the `text`
  114. field, and by the `english` analyzer` for the `text.english` field. The
  115. stemmed field allows a query for `foxes` to also match the document containing
  116. just `fox`. This allows us to match as many documents as possible. By also
  117. querying the unstemmed `text` field, we improve the relevance score of the
  118. document which matches `foxes` exactly.