all-field.asciidoc 8.7 KB

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