validate.asciidoc 6.0 KB

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