Explain API
editExplain API
editThe explain api computes a score explanation for a query and a specific document. This can give useful feedback whether a document matches or didn’t match a specific query.
Explain Request
editAn ExplainRequest
expects an index
, a type
and an id
to specify a certain document,
and a query represented by QueryBuilder
to run against it (the way of building queries).
ExplainRequest request = new ExplainRequest("contributors", "doc", "1"); request.query(QueryBuilders.termQuery("user", "tanguy"));
Optional arguments
edit
Use the preference parameter e.g. to execute the search to prefer local shards. The default is to randomize across shards. |
Set to true to retrieve the _source of the document explained. You can also retrieve part of the document by using _source_include & _source_exclude (see Get API for more details) |
Synchronous Execution
editThe explain
method executes the request synchronously:
ExplainResponse response = client.explain(request, RequestOptions.DEFAULT);
Asynchronous Execution
editThe explainAsync
method executes the request asynchronously,
calling the provided ActionListener
when the response is ready:
The asynchronous method does not block and returns immediately. Once the request
completes, the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed.
A typical listener for ExplainResponse
is constructed as follows:
ExplainResponse
editThe ExplainResponse
contains the following information:
String index = response.getIndex(); String type = response.getType(); String id = response.getId(); boolean exists = response.isExists(); boolean match = response.isMatch(); boolean hasExplanation = response.hasExplanation(); Explanation explanation = response.getExplanation(); GetResult getResult = response.getGetResult();
The index name of the explained document. |
|
The type name of the explained document. |
|
The id of the explained document. |
|
Indicates whether or not the explained document exists. |
|
Indicates whether or not there is a match between the explained document and
the provided query (the |
|
Indicates whether or not there exists a lucene |
|
Get the lucene |
|
Get the |
The GetResult
contains two maps internally to store the fetched _source
and stored fields.
You can use the following methods to get them: