all-field.asciidoc 8.7 KB

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