validate.asciidoc 6.6 KB

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