validate_query.asciidoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. [[java-rest-high-indices-validate-query]]
  2. === Validate Query API
  3. [[java-rest-high-indices-validate-query-request]]
  4. ==== Validate Query Request
  5. A `ValidateQueryRequest` requires one or more `indices` on which the query is validated. If no index
  6. is provided the request is executed on all indices.
  7. ["source","java",subs="attributes,callouts,macros"]
  8. --------------------------------------------------
  9. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request]
  10. --------------------------------------------------
  11. <1> The index on which to run the request.
  12. In addition it also needs the query that needs to be validated. The query can be built using the `QueryBuilders` utility class.
  13. The following code snippet builds a sample boolean query.
  14. ["source","java",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-query]
  17. --------------------------------------------------
  18. <1> Build the desired query.
  19. <2> Set it to the request.
  20. ==== Optional arguments
  21. The following arguments can optionally be provided:
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-explain]
  25. --------------------------------------------------
  26. <1> The explain parameter can be set to true to get more detailed information about why a query failed
  27. By default, the request is executed on a single shard only, which is randomly selected. The detailed explanation of
  28. the query may depend on which shard is being hit, and therefore may vary from one request to another. So, in case of
  29. query rewrite the `allShards` parameter should be used to get response from all available shards.
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-allShards]
  33. --------------------------------------------------
  34. <1> Set the allShards parameter.
  35. When the query is valid, the explanation defaults to the string representation of that query. With rewrite set to true,
  36. the explanation is more detailed showing the actual Lucene query that will be executed
  37. ["source","java",subs="attributes,callouts,macros"]
  38. --------------------------------------------------
  39. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-request-rewrite]
  40. --------------------------------------------------
  41. <1> Set the rewrite parameter.
  42. [[java-rest-high-indices-validate-query-sync]]
  43. ==== Synchronous Execution
  44. ["source","java",subs="attributes,callouts,macros"]
  45. --------------------------------------------------
  46. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute]
  47. --------------------------------------------------
  48. <1> Execute the request and get back the response in a ValidateQueryResponse object.
  49. [[java-rest-high-indices-validate-query-async]]
  50. ==== Asynchronous Execution
  51. The asynchronous execution of a validate query request requires both the `ValidateQueryRequest`
  52. instance and an `ActionListener` instance to be passed to the asynchronous
  53. method:
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute-async]
  57. --------------------------------------------------
  58. <1> The `ValidateQueryRequest` to execute and the `ActionListener` to use when
  59. the execution completes
  60. The asynchronous method does not block and returns immediately. Once it is
  61. completed the `ActionListener` is called back using the `onResponse` method
  62. if the execution successfully completed or using the `onFailure` method if
  63. it failed.
  64. A typical listener for `ValidateQueryResponse` looks like:
  65. ["source","java",subs="attributes,callouts,macros"]
  66. --------------------------------------------------
  67. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-execute-listener]
  68. --------------------------------------------------
  69. <1> Called when the execution is successfully completed. The response is
  70. provided as an argument
  71. <2> Called in case of failure. The raised exception is provided as an argument
  72. [[java-rest-high-indices-validate-query-response]]
  73. ==== Validate Query Response
  74. The returned `ValidateQueryResponse` allows to retrieve information about the executed
  75. operation as follows:
  76. ["source","java",subs="attributes,callouts,macros"]
  77. --------------------------------------------------
  78. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[validate-query-response]
  79. --------------------------------------------------
  80. <1> Check if the query is valid or not.
  81. <2> Get total number of shards.
  82. <3> Get number of shards that were successful.
  83. <4> Get number of shards that failed.
  84. <5> Get the shard failures as `DefaultShardOperationFailedException`.
  85. <6> Get the index of a failed shard.
  86. <7> Get the shard id of a failed shard.
  87. <8> Get the reason for shard failure.
  88. <9> Get the detailed explanation for the shards (if explain was set to `true`).
  89. <10> Get the index to which a particular explanation belongs.
  90. <11> Get the shard id to which a particular explanation belongs.
  91. <12> Get the actual explanation string.