all-field.asciidoc 9.1 KB

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