Validate API

Validates a potentially expensive query without executing it.

  1. GET my-index-000001/_validate/query?q=user.id:kimchy

Request

GET /<target>/_validate/<query>

Description

The validate API allows you to validate a potentially expensive query without executing it. The query can be sent either as a path parameter or in the request body.

Path parameters

<target>

(Optional, string) Comma-separated list of data streams, indices, and index aliases to search. Wildcard (*) expressions are supported.

To search all data streams or indices in a cluster, omit this parameter or use _all or *.

query

(Optional, query object) Defines the search definition using the Query DSL.

Query parameters

all_shards

(Optional, boolean) If true, the validation is executed on all shards instead of one random shard per index. Defaults to false.

allow_no_indices

(Optional, boolean) If true, the request does not return an error if a wildcard expression or _all value retrieves only missing or closed indices.

This parameter also applies to index aliases that point to a missing or closed index.

Defaults to false.

analyzer

(Optional, string) Analyzer to use for the query string.

analyze_wildcard

(Optional, boolean) If true, wildcard and prefix queries are analyzed. Defaults to false.

default_operator

(Optional, string) The default operator for query string query: AND or OR. Defaults to OR.

df

(Optional, string) Field to use as default where no field prefix is given in the query string.

expand_wildcards

(Optional, string) Controls what kind of indices that wildcard expressions can expand to. Multiple values are accepted when separated by a comma, as in open,hidden. Valid values are:

  • all

    Expand to open and closed indices, including hidden indices.

    open

    Expand only to open indices.

    closed

    Expand only to closed indices.

    hidden

    Expansion of wildcards will include hidden indices. Must be combined with open, closed, or both.

    none

    Wildcard expressions are not accepted.

explain

(Optional, boolean) If true, the response returns detailed information if an error has occurred. Defaults to false.

ignore_unavailable

(Optional, boolean) If true, missing or closed indices are not included in the response. Defaults to false.

lenient

(Optional, boolean) If true, format-based query failures (such as providing text to a numeric field) will be ignored. Defaults to false.

rewrite

(Optional, boolean) If true, returns a more detailed explanation showing the actual Lucene query that will be executed. Defaults to false.

q

(Optional, string) Query in the Lucene query string syntax.

Examples

  1. PUT my-index-000001/_bulk?refresh
  2. {"index":{"_id":1}}
  3. {"user" : { "id": "kimchy" }, "@timestamp" : "2099-11-15T14:12:12", "message" : "trying out Elasticsearch"}
  4. {"index":{"_id":2}}
  5. {"user" : { "id": "kimchi" }, "@timestamp" : "2099-11-15T14:12:13", "message" : "My user ID is similar to kimchy!"}

When sent a valid query:

  1. GET my-index-000001/_validate/query?q=user.id:kimchy

The response contains valid:true:

  1. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}

The query may also be sent in the request body:

  1. GET my-index-000001/_validate/query
  2. {
  3. "query" : {
  4. "bool" : {
  5. "must" : {
  6. "query_string" : {
  7. "query" : "*:*"
  8. }
  9. },
  10. "filter" : {
  11. "term" : { "user.id" : "kimchy" }
  12. }
  13. }
  14. }
  15. }

The query being sent in the body must be nested in a query key, same as the search api works

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:

  1. GET my-index-000001/_validate/query
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "@timestamp:foo",
  6. "lenient": false
  7. }
  8. }
  9. }
  1. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}

The explain parameter

An explain parameter can be specified to get more detailed information about why a query failed:

  1. GET my-index-000001/_validate/query?explain=true
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "@timestamp:foo",
  6. "lenient": false
  7. }
  8. }
  9. }

The API returns the following response:

  1. {
  2. "valid" : false,
  3. "_shards" : {
  4. "total" : 1,
  5. "successful" : 1,
  6. "failed" : 0
  7. },
  8. "explanations" : [ {
  9. "index" : "my-index-000001",
  10. "valid" : false,
  11. "error" : "my-index-000001/IAEc2nIXSSunQA_suI0MLw] QueryShardException[failed to create query:...failed to parse date field [foo]"
  12. } ]
  13. }

The rewrite parameter

When the query is valid, the explanation defaults to the string representation of that query. With rewrite set to true, the explanation is more detailed showing the actual Lucene query that will be executed.

  1. GET my-index-000001/_validate/query?rewrite=true
  2. {
  3. "query": {
  4. "more_like_this": {
  5. "like": {
  6. "_id": "2"
  7. },
  8. "boost_terms": 1
  9. }
  10. }
  11. }

The API returns the following response:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "my-index-000001",
  11. "valid": true,
  12. "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"
  13. }
  14. ]
  15. }

Rewrite and all_shards parameters

By default, the request is executed on a single shard only, which is randomly selected. The detailed explanation of the query may depend on which shard is being hit, and therefore may vary from one request to another. So, in case of query rewrite the all_shards parameter should be used to get response from all available shards.

  1. GET my-index-000001/_validate/query?rewrite=true&all_shards=true
  2. {
  3. "query": {
  4. "match": {
  5. "user.id": {
  6. "query": "kimchy",
  7. "fuzziness": "auto"
  8. }
  9. }
  10. }
  11. }

The API returns the following response:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "my-index-000001",
  11. "shard": 0,
  12. "valid": true,
  13. "explanation": "(user.id:kimchi)^0.8333333 user.id:kimchy"
  14. }
  15. ]
  16. }