all-field.asciidoc 10 KB

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