all-field.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // TEST[skip:_all is no longer allowed]
  87. // CONSOLE
  88. Other queries, such as the <<query-dsl-match-query,`match`>> and
  89. <<query-dsl-term-query,`term`>> queries require you to specify the `_all` field
  90. explicitly, as per the <<mapping-all-field,first example>>.
  91. [[enabling-all-field]]
  92. ==== Enabling the `_all` field
  93. The `_all` field can be enabled per-type by setting `enabled` to `true`:
  94. [source,js]
  95. --------------------------------
  96. PUT my_index
  97. {
  98. "mappings": {
  99. "type_1": { <1>
  100. "properties": {...}
  101. },
  102. "type_2": { <2>
  103. "_all": {
  104. "enabled": true
  105. },
  106. "properties": {...}
  107. }
  108. }
  109. }
  110. --------------------------------
  111. // TEST[s/\.\.\.//]
  112. // TEST[skip:_all is no longer allowed]
  113. // CONSOLE
  114. <1> The `_all` field in `type_1` is disabled.
  115. <2> The `_all` field in `type_2` is enabled.
  116. If the `_all` field is enabled, then URI search requests and the `query_string`
  117. and `simple_query_string` queries can automatically use it for queries (see
  118. <<querying-all-field>>). You can configure them to use a different field with
  119. the `index.query.default_field` setting:
  120. [source,js]
  121. --------------------------------
  122. PUT my_index
  123. {
  124. "mappings": {
  125. "my_type": {
  126. "properties": {
  127. "content": {
  128. "type": "text"
  129. }
  130. }
  131. }
  132. },
  133. "settings": {
  134. "index.query.default_field": "content" <1>
  135. }
  136. }
  137. --------------------------------
  138. // CONSOLE
  139. <1> The `query_string` query will default to querying the `content` field in this index.
  140. [[all-field-and-boosting]]
  141. ==== Index boosting and the `_all` field
  142. Individual fields can be _boosted_ at index time, with the <<mapping-boost,`boost`>>
  143. parameter. The `_all` field takes these boosts into account:
  144. [source,js]
  145. --------------------------------
  146. PUT myindex
  147. {
  148. "mappings": {
  149. "mytype": {
  150. "_all": {"enabled": true},
  151. "properties": {
  152. "title": { <1>
  153. "type": "text",
  154. "boost": 2
  155. },
  156. "content": { <1>
  157. "type": "text"
  158. }
  159. }
  160. }
  161. }
  162. }
  163. --------------------------------
  164. // TEST[skip:_all is no longer allowed]
  165. // CONSOLE
  166. <1> When querying the `_all` field, words that originated in the
  167. `title` field are twice as relevant as words that originated in
  168. the `content` field.
  169. WARNING: Using index-time boosting with the `_all` field has a significant
  170. impact on query performance. Usually the better solution is to query fields
  171. individually, with optional query time boosting.
  172. [[custom-all-fields]]
  173. ==== Custom `_all` fields
  174. While there is only a single `_all` field per index, the <<copy-to,`copy_to`>>
  175. parameter allows the creation of multiple __custom `_all` fields__. For
  176. instance, `first_name` and `last_name` fields can be combined together into
  177. the `full_name` field:
  178. [source,js]
  179. --------------------------------
  180. PUT myindex
  181. {
  182. "mappings": {
  183. "mytype": {
  184. "properties": {
  185. "first_name": {
  186. "type": "text",
  187. "copy_to": "full_name" <1>
  188. },
  189. "last_name": {
  190. "type": "text",
  191. "copy_to": "full_name" <1>
  192. },
  193. "full_name": {
  194. "type": "text"
  195. }
  196. }
  197. }
  198. }
  199. }
  200. PUT myindex/mytype/1
  201. {
  202. "first_name": "John",
  203. "last_name": "Smith"
  204. }
  205. GET myindex/_search
  206. {
  207. "query": {
  208. "match": {
  209. "full_name": "John Smith"
  210. }
  211. }
  212. }
  213. --------------------------------
  214. // CONSOLE
  215. <1> The `first_name` and `last_name` values are copied to the `full_name` field.
  216. [[highlighting-all-field]]
  217. ==== Highlighting and the `_all` field
  218. A field can only be used for <<search-request-highlighting,highlighting>> if
  219. the original string value is available, either from the
  220. <<mapping-source-field,`_source`>> field or as a stored field.
  221. The `_all` field is not present in the `_source` field and it is not stored or
  222. enabled by default, and so cannot be highlighted. There are two options. Either
  223. <<all-field-store,store the `_all` field>> or highlight the
  224. <<all-highlight-fields,original fields>>.
  225. [[all-field-store]]
  226. ===== Store the `_all` field
  227. If `store` is set to `true`, then the original field value is retrievable and
  228. can be highlighted:
  229. [source,js]
  230. --------------------------------
  231. PUT myindex
  232. {
  233. "mappings": {
  234. "mytype": {
  235. "_all": {
  236. "enabled": true,
  237. "store": true
  238. }
  239. }
  240. }
  241. }
  242. PUT myindex/mytype/1
  243. {
  244. "first_name": "John",
  245. "last_name": "Smith"
  246. }
  247. GET _search
  248. {
  249. "query": {
  250. "match": {
  251. "_all": "John Smith"
  252. }
  253. },
  254. "highlight": {
  255. "fields": {
  256. "_all": {}
  257. }
  258. }
  259. }
  260. --------------------------------
  261. // TEST[skip:_all is no longer allowed]
  262. // CONSOLE
  263. Of course, enabling and storing the `_all` field will use significantly more
  264. disk space and, because it is a combination of other fields, it may result in
  265. odd highlighting results.
  266. The `_all` field also accepts the `term_vector` and `index_options`
  267. parameters, allowing highlighting to use it.
  268. [[all-highlight-fields]]
  269. ===== Highlight original fields
  270. You can query the `_all` field, but use the original fields for highlighting as follows:
  271. [source,js]
  272. --------------------------------
  273. PUT myindex
  274. {
  275. "mappings": {
  276. "mytype": {
  277. "_all": {"enabled": true}
  278. }
  279. }
  280. }
  281. PUT myindex/mytype/1
  282. {
  283. "first_name": "John",
  284. "last_name": "Smith"
  285. }
  286. GET _search
  287. {
  288. "query": {
  289. "match": {
  290. "_all": "John Smith" <1>
  291. }
  292. },
  293. "highlight": {
  294. "fields": {
  295. "*_name": { <2>
  296. "require_field_match": false <3>
  297. }
  298. }
  299. }
  300. }
  301. --------------------------------
  302. // TEST[skip:_all is no longer allowed]
  303. // CONSOLE
  304. <1> The query inspects the `_all` field to find matching documents.
  305. <2> Highlighting is performed on the two name fields, which are available from the `_source`.
  306. <3> The query wasn't run against the name fields, so set `require_field_match` to `false`.