explain.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. [[java-rest-high-explain]]
  2. === Explain API
  3. The explain api computes a score explanation for a query and a specific document.
  4. This can give useful feedback whether a document matches or didn’t match a specific query.
  5. [[java-rest-high-explain-request]]
  6. ==== Explain Request
  7. An `ExplainRequest` expects an `index` and an `id` to specify a certain document,
  8. and a query represented by `QueryBuilder` to run against it (the way of <<java-rest-high-query-builders, building queries>>).
  9. ["source","java",subs="attributes,callouts,macros"]
  10. --------------------------------------------------
  11. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-request]
  12. --------------------------------------------------
  13. ===== Optional arguments
  14. ["source","java",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-request-routing]
  17. --------------------------------------------------
  18. <1> Set a routing parameter
  19. ["source","java",subs="attributes,callouts,macros"]
  20. --------------------------------------------------
  21. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-request-preference]
  22. --------------------------------------------------
  23. <1> Use the preference parameter e.g. to execute the search to prefer local
  24. shards. The default is to randomize across shards.
  25. ["source","java",subs="attributes,callouts,macros"]
  26. --------------------------------------------------
  27. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-request-source]
  28. --------------------------------------------------
  29. <1> Set to true to retrieve the _source of the document explained. You can also
  30. retrieve part of the document by using _source_include & _source_exclude
  31. (see <<java-rest-high-document-get-request-optional-arguments, Get API>> for more details)
  32. ["source","java",subs="attributes,callouts,macros"]
  33. --------------------------------------------------
  34. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-request-stored-field]
  35. --------------------------------------------------
  36. <1> Allows to control which stored fields to return as part of the document explained
  37. (requires the field to be stored separately in the mappings).
  38. [[java-rest-high-explain-sync]]
  39. ==== Synchronous Execution
  40. The `explain` method executes the request synchronously:
  41. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-execute]
  44. --------------------------------------------------
  45. [[java-rest-high-explain-async]]
  46. ==== Asynchronous Execution
  47. The `explainAsync` method executes the request asynchronously,
  48. calling the provided `ActionListener` when the response is ready:
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-execute-async]
  52. --------------------------------------------------
  53. <1> The `ExplainRequest` to execute and the `ActionListener` to use when
  54. the execution completes.
  55. The asynchronous method does not block and returns immediately. Once the request
  56. completes, the `ActionListener` is called back using the `onResponse` method
  57. if the execution successfully completed or using the `onFailure` method if
  58. it failed.
  59. A typical listener for `ExplainResponse` is constructed as follows:
  60. ["source","java",subs="attributes,callouts,macros"]
  61. --------------------------------------------------
  62. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-execute-listener]
  63. --------------------------------------------------
  64. <1> Called when the execution is successfully completed.
  65. <2> Called when the whole `ExplainRequest` fails.
  66. [[java-rest-high-explain-response]]
  67. ==== ExplainResponse
  68. The `ExplainResponse` contains the following information:
  69. ["source","java",subs="attributes,callouts,macros"]
  70. --------------------------------------------------
  71. include-tagged::{doc-tests}/SearchDocumentationIT.java[explain-response]
  72. --------------------------------------------------
  73. <1> The index name of the explained document.
  74. <2> The id of the explained document.
  75. <3> Indicates whether or not the explained document exists.
  76. <4> Indicates whether or not there is a match between the explained document and
  77. the provided query (the `match` is retrieved from the lucene `Explanation` behind the scenes
  78. if the lucene `Explanation` models a match, it returns `true`, otherwise it returns `false`).
  79. <5> Indicates whether or not there exists a lucene `Explanation` for this request.
  80. <6> Get the lucene `Explanation` object if there exists.
  81. <7> Get the `GetResult` object if the `_source` or the stored fields are retrieved.
  82. The `GetResult` contains two maps internally to store the fetched `_source` and stored fields.
  83. You can use the following methods to get them:
  84. ["source","java",subs="attributes,callouts,macros"]
  85. --------------------------------------------------
  86. include-tagged::{doc-tests}/SearchDocumentationIT.java[get-result]
  87. --------------------------------------------------
  88. <1> Retrieve the `_source` as a map.
  89. <2> Retrieve the specified stored fields as a map.