validate.asciidoc 6.0 KB

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