validate.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. |`lowercase_expanded_terms` |Should terms be automatically lowercased or
  43. not. Defaults to `true`.
  44. |`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
  45. not. Defaults to `false`.
  46. |=======================================================================
  47. The query may also be sent in the request body:
  48. [source,js]
  49. --------------------------------------------------
  50. GET twitter/tweet/_validate/query
  51. {
  52. "query" : {
  53. "bool" : {
  54. "must" : {
  55. "query_string" : {
  56. "query" : "*:*"
  57. }
  58. },
  59. "filter" : {
  60. "term" : { "user" : "kimchy" }
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // CONSOLE
  67. NOTE: The query being sent in the body must be nested in a `query` key, same as
  68. the <<search-search,search api>> works
  69. If the query is invalid, `valid` will be `false`. Here the query is
  70. invalid because Elasticsearch knows the post_date field should be a date
  71. due to dynamic mapping, and 'foo' does not correctly parse into a date:
  72. [source,js]
  73. --------------------------------------------------
  74. GET twitter/tweet/_validate/query?q=post_date:foo
  75. --------------------------------------------------
  76. // CONSOLE
  77. [source,js]
  78. --------------------------------------------------
  79. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}
  80. --------------------------------------------------
  81. // TESTRESPONSE
  82. An `explain` parameter can be specified to get more detailed information
  83. about why a query failed:
  84. [source,js]
  85. --------------------------------------------------
  86. GET twitter/tweet/_validate/query?q=post_date:foo&explain=true
  87. --------------------------------------------------
  88. // CONSOLE
  89. responds with:
  90. --------------------------------------------------
  91. {
  92. "valid" : false,
  93. "_shards" : {
  94. "total" : 1,
  95. "successful" : 1,
  96. "failed" : 0
  97. },
  98. "explanations" : [ {
  99. "index" : "twitter",
  100. "valid" : false,
  101. "error" : "twitter/IAEc2nIXSSunQA_suI0MLw] QueryShardException[failed to create query:...failed to parse date field [foo]"
  102. } ]
  103. }
  104. --------------------------------------------------
  105. // TESTRESPONSE[s/"error" : "[^\"]+"/"error": "$body.explanations.0.error"/]
  106. When the query is valid, the explanation defaults to the string
  107. representation of that query. With `rewrite` set to `true`, the explanation
  108. is more detailed showing the actual Lucene query that will be executed.
  109. For Fuzzy Queries:
  110. [source,js]
  111. --------------------------------------------------
  112. GET twitter/tweet/_validate/query?rewrite=true
  113. {
  114. "query": {
  115. "match": {
  116. "user": {
  117. "query": "kimchy",
  118. "fuzziness": "auto"
  119. }
  120. }
  121. }
  122. }
  123. --------------------------------------------------
  124. // CONSOLE
  125. // TEST[skip:https://github.com/elastic/elasticsearch/issues/18254]
  126. Response:
  127. [source,js]
  128. --------------------------------------------------
  129. {
  130. "valid": true,
  131. "_shards": {
  132. "total": 1,
  133. "successful": 1,
  134. "failed": 0
  135. },
  136. "explanations": [
  137. {
  138. "index": "twitter",
  139. "valid": true,
  140. "explanation": "+user:kimchy +user:kimchi^0.75 #(ConstantScore(_type:tweet))^0.0"
  141. }
  142. ]
  143. }
  144. --------------------------------------------------
  145. // TESTRESPONSE
  146. For More Like This:
  147. [source,js]
  148. --------------------------------------------------
  149. GET twitter/tweet/_validate/query?rewrite=true
  150. {
  151. "query": {
  152. "more_like_this": {
  153. "like": {
  154. "_id": "2"
  155. },
  156. "boost_terms": 1
  157. }
  158. }
  159. }
  160. --------------------------------------------------
  161. // CONSOLE
  162. // TEST[skip:https://github.com/elastic/elasticsearch/issues/18254]
  163. Response:
  164. [source,js]
  165. --------------------------------------------------
  166. {
  167. "valid": true,
  168. "_shards": {
  169. "total": 1,
  170. "successful": 1,
  171. "failed": 0
  172. },
  173. "explanations": [
  174. {
  175. "index": "twitter",
  176. "valid": true,
  177. "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"
  178. }
  179. ]
  180. }
  181. --------------------------------------------------
  182. // TESTRESPONSE
  183. CAUTION: The request is executed on a single shard only, which is randomly
  184. selected. The detailed explanation of the query may depend on which shard is
  185. being hit, and therefore may vary from one request to another.