validate.asciidoc 7.6 KB

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