validate.asciidoc 8.2 KB

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