all-field.asciidoc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. [[excluding-from-all]]
  130. ==== Excluding fields from `_all`
  131. Individual fields can be included or excluded from the `_all` field with the
  132. <<include-in-all,`include_in_all`>> setting.
  133. [[all-field-and-boosting]]
  134. ==== Index boosting and the `_all` field
  135. Individual fields can be _boosted_ at index time, with the <<index-boost,`boost`>>
  136. parameter. The `_all` field takes these boosts into account:
  137. [source,js]
  138. --------------------------------
  139. PUT myindex
  140. {
  141. "mappings": {
  142. "mytype": {
  143. "properties": {
  144. "title": { <1>
  145. "type": "string",
  146. "boost": 2
  147. },
  148. "content": { <1>
  149. "type": "string"
  150. }
  151. }
  152. }
  153. }
  154. }
  155. --------------------------------
  156. // AUTOSENSE
  157. <1> When querying the `_all` field, words that originated in the
  158. `title` field are twice as relevant as words that originated in
  159. the `content` field.
  160. WARNING: Using index-time boosting with the `_all` field has a significant
  161. impact on query performance. Usually the better solution is to query fields
  162. individually, with optional query time boosting.
  163. [[custom-all-fields]]
  164. ==== Custom `_all` fields
  165. While there is only a single `_all` field per index, the <<copy-to,`copy_to`>>
  166. parameter allows the creation of multiple __custom `_all` fields__. For
  167. instance, `first_name` and `last_name` fields can be combined together into
  168. the `full_name` field:
  169. [source,js]
  170. --------------------------------
  171. PUT myindex
  172. {
  173. "mappings": {
  174. "mytype": {
  175. "properties": {
  176. "first_name": {
  177. "type": "string",
  178. "copy_to": "full_name" <1>
  179. },
  180. "last_name": {
  181. "type": "string",
  182. "copy_to": "full_name" <1>
  183. },
  184. "full_name": {
  185. "type": "string"
  186. }
  187. }
  188. }
  189. }
  190. }
  191. PUT myindex/mytype/1
  192. {
  193. "first_name": "John",
  194. "last_name": "Smith"
  195. }
  196. GET myindex/_search
  197. {
  198. "query": {
  199. "match": {
  200. "full_name": "John Smith"
  201. }
  202. }
  203. }
  204. --------------------------------
  205. // AUTOSENSE
  206. <1> The `first_name` and `last_name` values are copied to the `full_name` field.
  207. [[highlighting-all-field]]
  208. ==== Highlighting and the `_all` field
  209. A field can only be used for <<search-request-highlighting,highlighting>> if
  210. the original string value is available, either from the
  211. <<mapping-source-field,`_source`>> field or as a stored field.
  212. The `_all` field is not present in the `_source` field and it is not stored by
  213. default, and so cannot be highlighted. There are two options. Either
  214. <<all-field-store,store the `_all` field>> or highlight the
  215. <<all-highlight-fields,original fields>>.
  216. [[all-field-store]]
  217. ===== Store the `_all` field
  218. If `store` is set to `true`, then the original field value is retrievable and
  219. can be highlighted:
  220. [source,js]
  221. --------------------------------
  222. PUT myindex
  223. {
  224. "mappings": {
  225. "mytype": {
  226. "_all": {
  227. "store": true
  228. }
  229. }
  230. }
  231. }
  232. PUT myindex/mytype/1
  233. {
  234. "first_name": "John",
  235. "last_name": "Smith"
  236. }
  237. GET _search
  238. {
  239. "query": {
  240. "match": {
  241. "_all": "John Smith"
  242. }
  243. },
  244. "highlight": {
  245. "fields": {
  246. "_all": {}
  247. }
  248. }
  249. }
  250. --------------------------------
  251. // AUTOSENSE
  252. Of course, storing the `_all` field will use significantly more disk space
  253. and, because it is a combination of other fields, it may result in odd
  254. highlighting results.
  255. The `_all` field also accepts the `term_vector` and `index_options`
  256. parameters, allowing the use of the fast vector highlighter and the postings
  257. highlighter.
  258. [[all-highlight-fields]]
  259. ===== Highlight original fields
  260. You can query the `_all` field, but use the original fields for highlighting as follows:
  261. [source,js]
  262. --------------------------------
  263. PUT myindex
  264. {
  265. "mappings": {
  266. "mytype": {
  267. "_all": {}
  268. }
  269. }
  270. }
  271. PUT myindex/mytype/1
  272. {
  273. "first_name": "John",
  274. "last_name": "Smith"
  275. }
  276. GET _search
  277. {
  278. "query": {
  279. "match": {
  280. "_all": "John Smith" <1>
  281. }
  282. },
  283. "highlight": {
  284. "fields": {
  285. "*_name": { <2>
  286. "require_field_match": "false" <3>
  287. }
  288. }
  289. }
  290. }
  291. --------------------------------
  292. // AUTOSENSE
  293. <1> The query inspects the `_all` field to find matching documents.
  294. <2> Highlighting is performed on the two name fields, which are available from the `_source`.
  295. <3> The query wasn't run against the name fields, so set `require_field_match` to `false`.