|
| 1 | +--- |
| 2 | +layout: guide |
| 3 | +title: Validate API |
| 4 | +cat: guide |
| 5 | +sidebar: reference_api |
| 6 | +--- |
| 7 | + |
| 8 | +p. The validate API allows a user to validate a potentially expensive query without executing it. The following example shows how it can be used: |
| 9 | + |
| 10 | +<pre class="prettyprint"> |
| 11 | +curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{ |
| 12 | + "user" : "kimchy", |
| 13 | + "post_date" : "2009-11-15T14:12:12", |
| 14 | + "message" : "trying out Elastic Search" |
| 15 | +}' |
| 16 | + |
| 17 | +</pre> |
| 18 | + |
| 19 | +When the query is valid, the response contains @valid:true@: |
| 20 | + |
| 21 | +<pre class="prettyprint"> |
| 22 | +curl -XGET 'http://localhost:9200/twitter/_validate/query?q=user:foo' |
| 23 | +{"valid":true,"_shards":{"total":1,"successful":1,"failed":0}} |
| 24 | +</pre> |
| 25 | + |
| 26 | +Or, with a request body: |
| 27 | + |
| 28 | +<pre class="prettyprint"> |
| 29 | +curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query' -d '{ |
| 30 | + "filtered" : { |
| 31 | + "query" : { |
| 32 | + "query_string" : { |
| 33 | + "query" : "*:*" |
| 34 | + } |
| 35 | + }, |
| 36 | + "filter" : { |
| 37 | + "term" : { "user" : "kimchy" } |
| 38 | + } |
| 39 | + } |
| 40 | +}' |
| 41 | +{"valid":true,"_shards":{"total":1,"successful":1,"failed":0}} |
| 42 | +</pre> |
| 43 | + |
| 44 | +If the query is invalid, 'valid' will be false. Here the query is invalid because ElasticSearch knows the post_date field should be a date due to dynamic mapping, and 'foo' does not correctly parse into a date: |
| 45 | + |
| 46 | +<pre class="prettyprint"> |
| 47 | +curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo' |
| 48 | +{"valid":false,"_shards":{"total":1,"successful":1,"failed":0}} |
| 49 | +</pre> |
| 50 | + |
| 51 | +An @explain@ parameter can be specified to get more detailed information about why a query failed: |
| 52 | + |
| 53 | +<pre class="prettyprint"> |
| 54 | +curl -XGET 'http://localhost:9200/twitter/tweet/_validate/query?q=post_date:foo&pretty=true&explain=true' |
| 55 | +{ |
| 56 | + "valid" : false, |
| 57 | + "_shards" : { |
| 58 | + "total" : 1, |
| 59 | + "successful" : 1, |
| 60 | + "failed" : 0 |
| 61 | + }, |
| 62 | + "explanations" : [ { |
| 63 | + "index" : "twitter", |
| 64 | + "valid" : false, |
| 65 | + "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\"" |
| 66 | + } ] |
| 67 | +} |
| 68 | +</pre> |
0 commit comments