validate.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. [[search-validate]]
  2. === Validate API
  3. ++++
  4. <titleabbrev>Validate</titleabbrev>
  5. ++++
  6. Validates a potentially expensive query without executing it.
  7. [source,console]
  8. --------------------------------------------------
  9. GET my-index-000001/_validate/query?q=user.id:kimchy
  10. --------------------------------------------------
  11. // TEST[setup:my_index]
  12. [[search-validate-api-request]]
  13. ==== {api-request-title}
  14. `GET /<target>/_validate/<query>`
  15. [[search-validate-api-prereqs]]
  16. ==== {api-prereq-title}
  17. * If the {es} {security-features} are enabled, you must have the `read`
  18. <<privileges-list-indices,index privilege>> for the target data stream, index,
  19. or index alias.
  20. [[search-validate-api-desc]]
  21. ==== {api-description-title}
  22. The validate API allows you to validate a potentially expensive query
  23. without executing it. The query can be sent either as a path parameter or in the
  24. request body.
  25. [[search-validate-api-path-params]]
  26. ==== {api-path-parms-title}
  27. `<target>`::
  28. (Optional, string)
  29. Comma-separated list of data streams, indices, and index aliases to search.
  30. Wildcard (`*`) expressions are supported.
  31. +
  32. To search all data streams or indices in a cluster, omit this parameter or use
  33. `_all` or `*`.
  34. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=query]
  35. [[search-validate-api-query-params]]
  36. ==== {api-query-parms-title}
  37. `all_shards`::
  38. (Optional, Boolean) If `true`, the validation is executed on all shards
  39. instead of one random shard per index. Defaults to `false`.
  40. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  41. +
  42. Defaults to `false`.
  43. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyzer]
  44. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  45. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=default_operator]
  46. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=df]
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  48. `explain`::
  49. (Optional, Boolean) If `true`, the response returns detailed information if an
  50. error has occurred. Defaults to `false`.
  51. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  52. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=lenient]
  53. `rewrite`::
  54. (Optional, Boolean) If `true`, returns a more detailed explanation showing the
  55. actual Lucene query that will be executed. Defaults to `false`.
  56. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  57. [[search-validate-api-example]]
  58. ==== {api-examples-title}
  59. [source,console]
  60. --------------------------------------------------
  61. PUT my-index-000001/_bulk?refresh
  62. {"index":{"_id":1}}
  63. {"user" : { "id": "kimchy" }, "@timestamp" : "2099-11-15T14:12:12", "message" : "trying out Elasticsearch"}
  64. {"index":{"_id":2}}
  65. {"user" : { "id": "kimchi" }, "@timestamp" : "2099-11-15T14:12:13", "message" : "My user ID is similar to kimchy!"}
  66. --------------------------------------------------
  67. When sent a valid query:
  68. [source,console]
  69. --------------------------------------------------
  70. GET my-index-000001/_validate/query?q=user.id:kimchy
  71. --------------------------------------------------
  72. // TEST[continued]
  73. The response contains `valid:true`:
  74. [source,console-result]
  75. --------------------------------------------------
  76. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}
  77. --------------------------------------------------
  78. The query may also be sent in the request body:
  79. [source,console]
  80. --------------------------------------------------
  81. GET my-index-000001/_validate/query
  82. {
  83. "query" : {
  84. "bool" : {
  85. "must" : {
  86. "query_string" : {
  87. "query" : "*:*"
  88. }
  89. },
  90. "filter" : {
  91. "term" : { "user.id" : "kimchy" }
  92. }
  93. }
  94. }
  95. }
  96. --------------------------------------------------
  97. // TEST[continued]
  98. NOTE: The query being sent in the body must be nested in a `query` key, same as
  99. the <<search-search,search api>> works
  100. If the query is invalid, `valid` will be `false`. Here the query is invalid
  101. because {es} knows the `post_date` field should be a date due to dynamic
  102. mapping, and 'foo' does not correctly parse into a date:
  103. [source,console]
  104. --------------------------------------------------
  105. GET my-index-000001/_validate/query
  106. {
  107. "query": {
  108. "query_string": {
  109. "query": "@timestamp:foo",
  110. "lenient": false
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TEST[continued]
  116. [source,console-result]
  117. --------------------------------------------------
  118. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}
  119. --------------------------------------------------
  120. ===== The explain parameter
  121. An `explain` parameter can be specified to get more detailed information about
  122. why a query failed:
  123. [source,console]
  124. --------------------------------------------------
  125. GET my-index-000001/_validate/query?explain=true
  126. {
  127. "query": {
  128. "query_string": {
  129. "query": "@timestamp:foo",
  130. "lenient": false
  131. }
  132. }
  133. }
  134. --------------------------------------------------
  135. // TEST[continued]
  136. The API returns the following response:
  137. [source,console-result]
  138. --------------------------------------------------
  139. {
  140. "valid" : false,
  141. "_shards" : {
  142. "total" : 1,
  143. "successful" : 1,
  144. "failed" : 0
  145. },
  146. "explanations" : [ {
  147. "index" : "my-index-000001",
  148. "valid" : false,
  149. "error" : "my-index-000001/IAEc2nIXSSunQA_suI0MLw] QueryShardException[failed to create query:...failed to parse date field [foo]"
  150. } ]
  151. }
  152. --------------------------------------------------
  153. // TESTRESPONSE[s/"error" : "[^\"]+"/"error": "$body.explanations.0.error"/]
  154. ===== The rewrite parameter
  155. When the query is valid, the explanation defaults to the string representation
  156. of that query. With `rewrite` set to `true`, the explanation is more detailed
  157. showing the actual Lucene query that will be executed.
  158. [source,console]
  159. --------------------------------------------------
  160. GET my-index-000001/_validate/query?rewrite=true
  161. {
  162. "query": {
  163. "more_like_this": {
  164. "like": {
  165. "_id": "2"
  166. },
  167. "boost_terms": 1
  168. }
  169. }
  170. }
  171. --------------------------------------------------
  172. // TEST[skip:the output is randomized depending on which shard we hit]
  173. The API returns the following response:
  174. [source,console-result]
  175. --------------------------------------------------
  176. {
  177. "valid": true,
  178. "_shards": {
  179. "total": 1,
  180. "successful": 1,
  181. "failed": 0
  182. },
  183. "explanations": [
  184. {
  185. "index": "my-index-000001",
  186. "valid": true,
  187. "explanation": "((user:terminator^3.71334 plot:future^2.763601 plot:human^2.8415773 plot:sarah^3.4193945 plot:kyle^3.8244398 plot:cyborg^3.9177752 plot:connor^4.040236 plot:reese^4.7133346 ... )~6) -ConstantScore(_id:2)) #(ConstantScore(_type:_doc))^0.0"
  188. }
  189. ]
  190. }
  191. --------------------------------------------------
  192. ===== Rewrite and all_shards parameters
  193. By default, the request is executed on a single shard only, which is randomly
  194. selected. The detailed explanation of the query may depend on which shard is
  195. being hit, and therefore may vary from one request to another. So, in case of
  196. query rewrite the `all_shards` parameter should be used to get response from
  197. all available shards.
  198. ////
  199. [source,console]
  200. --------------------------------------------------
  201. PUT my-index-000001/_bulk?refresh
  202. {"index":{"_id":1}}
  203. {"user" : { "id": "kimchy" }, "@timestamp" : "2099-11-15T14:12:12", "message" : "trying out Elasticsearch"}
  204. {"index":{"_id":2}}
  205. {"user" : { "id": "kimchi" }, "@timestamp" : "2099-11-15T14:12:13", "message" : "My user ID is similar to kimchy!"}
  206. --------------------------------------------------
  207. ////
  208. [source,console]
  209. --------------------------------------------------
  210. GET my-index-000001/_validate/query?rewrite=true&all_shards=true
  211. {
  212. "query": {
  213. "match": {
  214. "user.id": {
  215. "query": "kimchy",
  216. "fuzziness": "auto"
  217. }
  218. }
  219. }
  220. }
  221. --------------------------------------------------
  222. // TEST[continued]
  223. The API returns the following response:
  224. [source,console-result]
  225. --------------------------------------------------
  226. {
  227. "valid": true,
  228. "_shards": {
  229. "total": 1,
  230. "successful": 1,
  231. "failed": 0
  232. },
  233. "explanations": [
  234. {
  235. "index": "my-index-000001",
  236. "shard": 0,
  237. "valid": true,
  238. "explanation": "(user.id:kimchi)^0.8333333 user.id:kimchy"
  239. }
  240. ]
  241. }
  242. --------------------------------------------------