all-field.asciidoc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. "place_of_birth": "New York City"
  17. }
  18. GET my_index/_search
  19. {
  20. "query": {
  21. "match": {
  22. "_all": "john smith new york"
  23. }
  24. }
  25. }
  26. --------------------------------
  27. // CONSOLE
  28. <1> The `_all` field will contain the terms: [ `"john"`, `"smith"`, `"new"`, `"york"`, `"city"` ]
  29. [NOTE]
  30. .Only string values are added to _all
  31. =============================================================================
  32. If a `date_of_birth` field mapped as a date were used, or an `age` field that
  33. was an integer were added, they would not be included in the `_all` field, as
  34. `_all` only contains content from _string_ fields.
  35. It is important to note that the `_all` field combines the original values
  36. from each field as a string. It does not combine the _terms_ from each field.
  37. =============================================================================
  38. The `_all` field is just a <<text,`text`>> field, and accepts the same
  39. parameters that other string fields accept, including `analyzer`,
  40. `term_vectors`, `index_options`, and `store`.
  41. The `_all` field can be useful, especially when exploring new data using
  42. simple filtering. However, by concatenating field values into one big string,
  43. the `_all` field loses the distinction between short fields (more relevant)
  44. and long fields (less relevant). For use cases where search relevance is
  45. important, it is better to query individual fields specifically.
  46. The `_all` field is not free: it requires extra CPU cycles and uses more disk
  47. space. If not needed, it can be completely <<disabling-all-field,disabled>> or
  48. customised on a <<include-in-all,per-field basis>>.
  49. [[querying-all-field]]
  50. ==== Using the `_all` field in queries
  51. The <<query-dsl-query-string-query,`query_string`>> and
  52. <<query-dsl-simple-query-string-query,`simple_query_string`>> queries query
  53. the `_all` field by default, unless another field is specified:
  54. [source,js]
  55. --------------------------------
  56. GET _search
  57. {
  58. "query": {
  59. "query_string": {
  60. "query": "john smith new york"
  61. }
  62. }
  63. }
  64. --------------------------------
  65. // CONSOLE
  66. The same goes for the `?q=` parameter in <<search-uri-request, URI search
  67. requests>> (which is rewritten to a `query_string` query internally):
  68. [source,js]
  69. --------------------------------
  70. GET _search?q=john+smith+new+york
  71. --------------------------------
  72. Other queries, such as the <<query-dsl-match-query,`match`>> and
  73. <<query-dsl-term-query,`term`>> queries require you to specify
  74. the `_all` field explicitly, as per the
  75. <<mapping-all-field,first example>>.
  76. [[disabling-all-field]]
  77. ==== Disabling the `_all` field
  78. The `_all` field can be completely disabled per-type by setting `enabled` to
  79. `false`:
  80. [source,js]
  81. --------------------------------
  82. PUT my_index
  83. {
  84. "mappings": {
  85. "type_1": { <1>
  86. "properties": {...}
  87. },
  88. "type_2": { <2>
  89. "_all": {
  90. "enabled": false
  91. },
  92. "properties": {...}
  93. }
  94. }
  95. }
  96. --------------------------------
  97. // CONSOLE
  98. // TEST[s/\.\.\.//]
  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": "text"
  117. }
  118. }
  119. }
  120. },
  121. "settings": {
  122. "index.query.default_field": "content" <2>
  123. }
  124. }
  125. --------------------------------
  126. // CONSOLE
  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 <<mapping-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": "text",
  146. "boost": 2
  147. },
  148. "content": { <1>
  149. "type": "text"
  150. }
  151. }
  152. }
  153. }
  154. }
  155. --------------------------------
  156. // CONSOLE
  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": "text",
  178. "copy_to": "full_name" <1>
  179. },
  180. "last_name": {
  181. "type": "text",
  182. "copy_to": "full_name" <1>
  183. },
  184. "full_name": {
  185. "type": "text"
  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. // CONSOLE
  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. // CONSOLE
  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. // CONSOLE
  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`.