validate.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. [[search-validate]]
  2. == Validate API
  3. The validate API allows a user to validate a potentially expensive query
  4. without executing it. The following example shows how it can be used:
  5. [source,js]
  6. --------------------------------------------------
  7. curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
  8. "user" : "kimchy",
  9. "post_date" : "2009-11-15T14:12:12",
  10. "message" : "trying out Elasticsearch"
  11. }'
  12. --------------------------------------------------
  13. When the query is valid, the response contains `valid:true`:
  14. [source,js]
  15. --------------------------------------------------
  16. curl -XGET 'http://localhost:9200/twitter/_validate/query?q=user:foo'
  17. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}
  18. --------------------------------------------------
  19. [float]
  20. === Request Parameters
  21. When executing exists using the query parameter `q`, the query passed is
  22. a query string using Lucene query parser. There are additional
  23. parameters that can be passed:
  24. [cols="<,<",options="header",]
  25. |=======================================================================
  26. |Name |Description
  27. |`df` |The default field to use when no field prefix is defined within the
  28. query.
  29. |`analyzer` |The analyzer name to be used when analyzing the query string.
  30. |`default_operator` |The default operator to be used, can be `AND` or
  31. `OR`. Defaults to `OR`.
  32. |`lenient` |If set to true will cause format based failures (like
  33. providing text to a numeric field) to be ignored. Defaults to false.
  34. |`lowercase_expanded_terms` |Should terms be automatically lowercased or
  35. not. Defaults to `true`.
  36. |`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
  37. not. Defaults to `false`.
  38. |=======================================================================
  39. Or, with a request body:
  40. [source,js]
  41. --------------------------------------------------
  42. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{
  43. "query" : {
  44. "bool" : {
  45. "must" : {
  46. "query_string" : {
  47. "query" : "*:*"
  48. }
  49. },
  50. "filter" : {
  51. "term" : { "user" : "kimchy" }
  52. }
  53. }
  54. }
  55. }'
  56. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}
  57. --------------------------------------------------
  58. NOTE: The query being sent in the body must be nested in a `query` key, same as
  59. the <<search-search,search api>> works
  60. If the query is invalid, `valid` will be `false`. Here the query is
  61. invalid because Elasticsearch knows the post_date field should be a date
  62. due to dynamic mapping, and 'foo' does not correctly parse into a date:
  63. [source,js]
  64. --------------------------------------------------
  65. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo'
  66. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}
  67. --------------------------------------------------
  68. An `explain` parameter can be specified to get more detailed information
  69. about why a query failed:
  70. [source,js]
  71. --------------------------------------------------
  72. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo&pretty=true&explain=true'
  73. {
  74. "valid" : false,
  75. "_shards" : {
  76. "total" : 1,
  77. "successful" : 1,
  78. "failed" : 0
  79. },
  80. "explanations" : [ {
  81. "index" : "twitter",
  82. "valid" : false,
  83. "error" : "[twitter] QueryParsingException[Failed to parse]; nested: IllegalArgumentException[Invalid format: \"foo\"];; java.lang.IllegalArgumentException: Invalid format: \"foo\""
  84. } ]
  85. }
  86. --------------------------------------------------
  87. When the query is valid, the explanation defaults to the string
  88. representation of that query. With `rewrite` set to `true`, the explanation
  89. is more detailed showing the actual Lucene query that will be executed.
  90. For Fuzzy Queries:
  91. [source,js]
  92. --------------------------------------------------
  93. curl -XGET 'http://localhost:9200/imdb/movies/_validate/query?rewrite=true' -d '
  94. {
  95. "query": {
  96. "fuzzy": {
  97. "actors": "kyle"
  98. }
  99. }
  100. }'
  101. --------------------------------------------------
  102. Response:
  103. [source,js]
  104. --------------------------------------------------
  105. {
  106. "valid": true,
  107. "_shards": {
  108. "total": 1,
  109. "successful": 1,
  110. "failed": 0
  111. },
  112. "explanations": [
  113. {
  114. "index": "imdb",
  115. "valid": true,
  116. "explanation": "plot:kyle plot:kylie^0.75 plot:kyne^0.75 plot:lyle^0.75 plot:pyle^0.75 #_type:movies"
  117. }
  118. ]
  119. }
  120. --------------------------------------------------
  121. For More Like This:
  122. [source,js]
  123. --------------------------------------------------
  124. curl -XGET 'http://localhost:9200/imdb/movies/_validate/query?rewrite=true'
  125. {
  126. "query": {
  127. "more_like_this": {
  128. "like": {
  129. "_id": "88247"
  130. },
  131. "boost_terms": 1
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. Response:
  137. [source,js]
  138. --------------------------------------------------
  139. {
  140. "valid": true,
  141. "_shards": {
  142. "total": 1,
  143. "successful": 1,
  144. "failed": 0
  145. },
  146. "explanations": [
  147. {
  148. "index": "imdb",
  149. "valid": true,
  150. "explanation": "((title: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:movies#88247) #_type:movies"
  151. }
  152. ]
  153. }
  154. --------------------------------------------------
  155. CAUTION: 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.