all-field.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. [[mapping-all-field]]
  2. === `_all` field
  3. deprecated[6.0.0, `_all` may no longer be enabled for indices created in 6.0+, use a custom field and the mapping `copy_to` parameter]
  4. The `_all` field is a special _catch-all_ field which concatenates the values
  5. of all of the other fields into one big string, using space as a delimiter, which is then
  6. <<analysis,analyzed>> and indexed, but not stored. This means that it can be
  7. searched, but not retrieved.
  8. The `_all` field allows you to search for values in documents without knowing
  9. which field contains the value. This makes it a useful option when getting
  10. started with a new dataset. For instance:
  11. [source,js]
  12. --------------------------------
  13. PUT /my_index
  14. {
  15. "mapping": {
  16. "user": {
  17. "_all": {
  18. "enabled": true <1>
  19. }
  20. }
  21. }
  22. }
  23. PUT /my_index/user/1 <2>
  24. {
  25. "first_name": "John",
  26. "last_name": "Smith",
  27. "date_of_birth": "1970-10-24"
  28. }
  29. GET /my_index/_search
  30. {
  31. "query": {
  32. "match": {
  33. "_all": "john smith 1970"
  34. }
  35. }
  36. }
  37. --------------------------------
  38. // TEST[skip:_all is no longer allowed]
  39. // CONSOLE
  40. <1> Enabling the `_all` field
  41. <2> The `_all` field will contain the terms: [ `"john"`, `"smith"`, `"1970"`, `"10"`, `"24"` ]
  42. [NOTE]
  43. .All values treated as strings
  44. =============================================================================
  45. The `date_of_birth` field in the above example is recognised as a `date` field
  46. and so will index a single term representing `1970-10-24 00:00:00 UTC`. The
  47. `_all` field, however, treats all values as strings, so the date value is
  48. indexed as the three string terms: `"1970"`, `"24"`, `"10"`.
  49. It is important to note that the `_all` field combines the original values
  50. from each field as a string. It does not combine the _terms_ from each field.
  51. =============================================================================
  52. The `_all` field is just a <<text,`text`>> field, and accepts the same
  53. parameters that other string fields accept, including `analyzer`,
  54. `term_vectors`, `index_options`, and `store`.
  55. The `_all` field can be useful, especially when exploring new data using
  56. simple filtering. However, by concatenating field values into one big string,
  57. the `_all` field loses the distinction between short fields (more relevant)
  58. and long fields (less relevant). For use cases where search relevance is
  59. important, it is better to query individual fields specifically.
  60. The `_all` field is not free: it requires extra CPU cycles and uses more disk
  61. space. For this reason, it is disabled by default. If needed, it can be
  62. <<enabling-all-field,enabled>>.
  63. [[querying-all-field]]
  64. ==== Using the `_all` field in queries
  65. The <<query-dsl-query-string-query,`query_string`>> and
  66. <<query-dsl-simple-query-string-query,`simple_query_string`>> queries query the
  67. `_all` field by default if it is enabled, unless another field is specified:
  68. [source,js]
  69. --------------------------------
  70. GET _search
  71. {
  72. "query": {
  73. "query_string": {
  74. "query": "john smith new york"
  75. }
  76. }
  77. }
  78. --------------------------------
  79. // CONSOLE
  80. The same goes for the `?q=` parameter in <<search-uri-request, URI search
  81. requests>> (which is rewritten to a `query_string` query internally):
  82. [source,js]
  83. --------------------------------
  84. GET _search?q=john+smith+new+york
  85. --------------------------------
  86. Other queries, such as the <<query-dsl-match-query,`match`>> and
  87. <<query-dsl-term-query,`term`>> queries require you to specify the `_all` field
  88. explicitly, as per the <<mapping-all-field,first example>>.
  89. [[enabling-all-field]]
  90. ==== Enabling the `_all` field
  91. The `_all` field can be enabled per-type by setting `enabled` to `true`:
  92. [source,js]
  93. --------------------------------
  94. PUT my_index
  95. {
  96. "mappings": {
  97. "type_1": { <1>
  98. "properties": {...}
  99. },
  100. "type_2": { <2>
  101. "_all": {
  102. "enabled": true
  103. },
  104. "properties": {...}
  105. }
  106. }
  107. }
  108. --------------------------------
  109. // TEST[s/\.\.\.//]
  110. // TEST[skip:_all is no longer allowed]
  111. // CONSOLE
  112. <1> The `_all` field in `type_1` is disabled.
  113. <2> The `_all` field in `type_2` is enabled.
  114. If the `_all` field is enabled, then URI search requests and the `query_string`
  115. and `simple_query_string` queries can automatically use it for queries (see
  116. <<querying-all-field>>). You can configure them to use a different field with
  117. the `index.query.default_field` setting:
  118. [source,js]
  119. --------------------------------
  120. PUT my_index
  121. {
  122. "mappings": {
  123. "my_type": {
  124. "properties": {
  125. "content": {
  126. "type": "text"
  127. }
  128. }
  129. }
  130. },
  131. "settings": {
  132. "index.query.default_field": "content" <1>
  133. }
  134. }
  135. --------------------------------
  136. // CONSOLE
  137. <1> The `query_string` query will default to querying the `content` field in this index.
  138. [[all-field-and-boosting]]
  139. ==== Index boosting and the `_all` field
  140. Individual fields can be _boosted_ at index time, with the <<mapping-boost,`boost`>>
  141. parameter. The `_all` field takes these boosts into account:
  142. [source,js]
  143. --------------------------------
  144. PUT myindex
  145. {
  146. "mappings": {
  147. "mytype": {
  148. "_all": {"enabled": true},
  149. "properties": {
  150. "title": { <1>
  151. "type": "text",
  152. "boost": 2
  153. },
  154. "content": { <1>
  155. "type": "text"
  156. }
  157. }
  158. }
  159. }
  160. }
  161. --------------------------------
  162. // TEST[skip:_all is no longer allowed]
  163. // CONSOLE
  164. <1> When querying the `_all` field, words that originated in the
  165. `title` field are twice as relevant as words that originated in
  166. the `content` field.
  167. WARNING: Using index-time boosting with the `_all` field has a significant
  168. impact on query performance. Usually the better solution is to query fields
  169. individually, with optional query time boosting.
  170. [[custom-all-fields]]
  171. ==== Custom `_all` fields
  172. While there is only a single `_all` field per index, the <<copy-to,`copy_to`>>
  173. parameter allows the creation of multiple __custom `_all` fields__. For
  174. instance, `first_name` and `last_name` fields can be combined together into
  175. the `full_name` field:
  176. [source,js]
  177. --------------------------------
  178. PUT myindex
  179. {
  180. "mappings": {
  181. "mytype": {
  182. "properties": {
  183. "first_name": {
  184. "type": "text",
  185. "copy_to": "full_name" <1>
  186. },
  187. "last_name": {
  188. "type": "text",
  189. "copy_to": "full_name" <1>
  190. },
  191. "full_name": {
  192. "type": "text"
  193. }
  194. }
  195. }
  196. }
  197. }
  198. PUT myindex/mytype/1
  199. {
  200. "first_name": "John",
  201. "last_name": "Smith"
  202. }
  203. GET myindex/_search
  204. {
  205. "query": {
  206. "match": {
  207. "full_name": "John Smith"
  208. }
  209. }
  210. }
  211. --------------------------------
  212. // CONSOLE
  213. <1> The `first_name` and `last_name` values are copied to the `full_name` field.
  214. [[highlighting-all-field]]
  215. ==== Highlighting and the `_all` field
  216. A field can only be used for <<search-request-highlighting,highlighting>> if
  217. the original string value is available, either from the
  218. <<mapping-source-field,`_source`>> field or as a stored field.
  219. The `_all` field is not present in the `_source` field and it is not stored or
  220. enabled by default, and so cannot be highlighted. There are two options. Either
  221. <<all-field-store,store the `_all` field>> or highlight the
  222. <<all-highlight-fields,original fields>>.
  223. [[all-field-store]]
  224. ===== Store the `_all` field
  225. If `store` is set to `true`, then the original field value is retrievable and
  226. can be highlighted:
  227. [source,js]
  228. --------------------------------
  229. PUT myindex
  230. {
  231. "mappings": {
  232. "mytype": {
  233. "_all": {
  234. "enabled": true,
  235. "store": true
  236. }
  237. }
  238. }
  239. }
  240. PUT myindex/mytype/1
  241. {
  242. "first_name": "John",
  243. "last_name": "Smith"
  244. }
  245. GET _search
  246. {
  247. "query": {
  248. "match": {
  249. "_all": "John Smith"
  250. }
  251. },
  252. "highlight": {
  253. "fields": {
  254. "_all": {}
  255. }
  256. }
  257. }
  258. --------------------------------
  259. // TEST[skip:_all is no longer allowed]
  260. // CONSOLE
  261. Of course, enabling and storing the `_all` field will use significantly more
  262. disk space and, because it is a combination of other fields, it may result in
  263. odd highlighting results.
  264. The `_all` field also accepts the `term_vector` and `index_options`
  265. parameters, allowing the use of the fast vector highlighter and the postings
  266. highlighter.
  267. [[all-highlight-fields]]
  268. ===== Highlight original fields
  269. You can query the `_all` field, but use the original fields for highlighting as follows:
  270. [source,js]
  271. --------------------------------
  272. PUT myindex
  273. {
  274. "mappings": {
  275. "mytype": {
  276. "_all": {"enabled": true}
  277. }
  278. }
  279. }
  280. PUT myindex/mytype/1
  281. {
  282. "first_name": "John",
  283. "last_name": "Smith"
  284. }
  285. GET _search
  286. {
  287. "query": {
  288. "match": {
  289. "_all": "John Smith" <1>
  290. }
  291. },
  292. "highlight": {
  293. "fields": {
  294. "*_name": { <2>
  295. "require_field_match": false <3>
  296. }
  297. }
  298. }
  299. }
  300. --------------------------------
  301. // TEST[skip:_all is no longer allowed]
  302. // CONSOLE
  303. <1> The query inspects the `_all` field to find matching documents.
  304. <2> Highlighting is performed on the two name fields, which are available from the `_source`.
  305. <3> The query wasn't run against the name fields, so set `require_field_match` to `false`.