| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | [[java-rest-high-indices-validate-query]]=== Validate Query API[[java-rest-high-indices-validate-query-request]]==== Validate Query RequestA `ValidateQueryRequest` requires one or more `indices` on which the query is validated. If no indexis provided the request is executed on all indices.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request]--------------------------------------------------<1> The index on which to run the request.In addition it also needs the query that needs to be validated. The query can be built using the `QueryBuilders` utility class.The following code snippet builds a sample boolean query.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-query]--------------------------------------------------<1> Build the desired query.<2> Set it to the request.==== Optional argumentsThe following arguments can optionally be provided:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-explain]--------------------------------------------------<1> The explain parameter can be set to true to get more detailed information about why a query failedBy default, the request is executed on a single shard only, which is randomly selected. The detailed explanation ofthe query may depend on which shard is being hit, and therefore may vary from one request to another. So, in case ofquery rewrite the `allShards` parameter should be used to get response from all available shards.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-allShards]--------------------------------------------------<1> Set the allShards 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["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-rewrite]--------------------------------------------------<1> Set the rewrite parameter.[[java-rest-high-indices-validate-query-sync]]==== Synchronous Execution["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute]--------------------------------------------------<1> Execute the request and get back the response in a ValidateQueryResponse object.[[java-rest-high-indices-validate-query-async]]==== Asynchronous ExecutionThe asynchronous execution of a validate query request requires both the `ValidateQueryRequest`instance and an `ActionListener` instance to be passed to the asynchronousmethod:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute-async]--------------------------------------------------<1> The `ValidateQueryRequest` to execute and the `ActionListener` to use whenthe execution completesThe asynchronous method does not block and returns immediately. Once it iscompleted the `ActionListener` is called back using the `onResponse` methodif the execution successfully completed or using the `onFailure` method ifit failed.A typical listener for `ValidateQueryResponse` looks like:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute-listener]--------------------------------------------------<1> Called when the execution is successfully completed. The response isprovided as an argument<2> Called in case of failure. The raised exception is provided as an argument[[java-rest-high-indices-validate-query-response]]==== Validate Query ResponseThe returned `ValidateQueryResponse` allows to retrieve information about the executed operation as follows:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-response]--------------------------------------------------<1> Check if the query is valid or not.<2> Get total number of shards.<3> Get number of shards that were successful.<4> Get number of shards that failed.<5> Get the shard failures as `DefaultShardOperationFailedException`.<6> Get the index of a failed shard.<7> Get the shard id of a failed shard.<8> Get the reason for shard failure.<9> Get the detailed explanation for the shards (if explain was set to `true`).<10> Get the index to which a particular explanation belongs.<11> Get the shard id to which a particular explanation belongs.<12> Get the actual explanation string.
 |