validate.asciidoc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Or, with a request body:
  20. [source,js]
  21. --------------------------------------------------
  22. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{
  23. "query" : {
  24. "filtered" : {
  25. "query" : {
  26. "query_string" : {
  27. "query" : "*:*"
  28. }
  29. },
  30. "filter" : {
  31. "term" : { "user" : "kimchy" }
  32. }
  33. }
  34. }
  35. }'
  36. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}
  37. --------------------------------------------------
  38. NOTE: The query being sent in the body must be nested in a `query` key, same as
  39. the <<search-search,search api>> works added[1.0.0.RC1,The query was previously the top-level object].
  40. If the query is invalid, `valid` will be `false`. Here the query is
  41. invalid because Elasticsearch knows the post_date field should be a date
  42. due to dynamic mapping, and 'foo' does not correctly parse into a date:
  43. [source,js]
  44. --------------------------------------------------
  45. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo'
  46. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}
  47. --------------------------------------------------
  48. An `explain` parameter can be specified to get more detailed information
  49. about why a query failed:
  50. [source,js]
  51. --------------------------------------------------
  52. curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo&pretty=true&explain=true'
  53. {
  54. "valid" : false,
  55. "_shards" : {
  56. "total" : 1,
  57. "successful" : 1,
  58. "failed" : 0
  59. },
  60. "explanations" : [ {
  61. "index" : "twitter",
  62. "valid" : false,
  63. "error" : "org.elasticsearch.index.query.QueryParsingException: [twitter] Failed to parse; org.elasticsearch.ElasticsearchParseException: failed to parse date field [foo], tried both date format [dateOptionalTime], and timestamp number; java.lang.IllegalArgumentException: Invalid format: \"foo\""
  64. } ]
  65. }
  66. --------------------------------------------------