validate.asciidoc 8.0 KB

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