multi-fields.asciidoc 3.3 KB

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