validate.asciidoc 7.5 KB

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