use of org.opensearch.action.explain.ExplainRequest in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testExplain.
public void testExplain() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
// tag::explain-request
ExplainRequest request = new ExplainRequest("contributors", "1");
request.query(QueryBuilders.termQuery("user", "quuz"));
// end::explain-request
// tag::explain-request-routing
// <1>
request.routing("routing");
// end::explain-request-routing
// tag::explain-request-preference
// <1>
request.preference("_local");
// end::explain-request-preference
// tag::explain-request-source
// <1>
request.fetchSourceContext(new FetchSourceContext(true, new String[] { "user" }, null));
// end::explain-request-source
// tag::explain-request-stored-field
// <1>
request.storedFields(new String[] { "user" });
// end::explain-request-stored-field
// tag::explain-execute
ExplainResponse response = client.explain(request, RequestOptions.DEFAULT);
// end::explain-execute
// tag::explain-response
// <1>
String index = response.getIndex();
// <2>
String id = response.getId();
// <3>
boolean exists = response.isExists();
// <4>
boolean match = response.isMatch();
// <5>
boolean hasExplanation = response.hasExplanation();
// <6>
Explanation explanation = response.getExplanation();
// <7>
GetResult getResult = response.getGetResult();
// end::explain-response
assertThat(index, equalTo("contributors"));
assertThat(id, equalTo("1"));
assertTrue(exists);
assertTrue(match);
assertTrue(hasExplanation);
assertNotNull(explanation);
assertNotNull(getResult);
// tag::get-result
// <1>
Map<String, Object> source = getResult.getSource();
// <2>
Map<String, DocumentField> fields = getResult.getFields();
// end::get-result
assertThat(source, equalTo(Collections.singletonMap("user", "quuz")));
assertThat(fields.get("user").getValue(), equalTo("quuz"));
// tag::explain-execute-listener
ActionListener<ExplainResponse> listener = new ActionListener<ExplainResponse>() {
@Override
public void onResponse(ExplainResponse explainResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::explain-execute-listener
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::explain-execute-async
// <1>
client.explainAsync(request, RequestOptions.DEFAULT, listener);
// end::explain-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.action.explain.ExplainRequest in project OpenSearch by opensearch-project.
the class IndicesRequestIT method testExplain.
public void testExplain() {
String explainShardAction = ExplainAction.NAME + "[s]";
interceptTransportActions(explainShardAction);
ExplainRequest explainRequest = new ExplainRequest(randomIndexOrAlias(), "id").query(QueryBuilders.matchAllQuery());
internalCluster().coordOnlyNodeClient().explain(explainRequest).actionGet();
clearInterceptedActions();
assertSameIndices(explainRequest, explainShardAction);
}
use of org.opensearch.action.explain.ExplainRequest in project OpenSearch by opensearch-project.
the class SearchIT method testExplainWithAliasFilter.
public void testExplainWithAliasFilter() throws IOException {
ExplainRequest explainRequest = new ExplainRequest("alias4", "1");
explainRequest.query(QueryBuilders.matchAllQuery());
ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync);
assertTrue(explainResponse.isExists());
assertFalse(explainResponse.isMatch());
}
use of org.opensearch.action.explain.ExplainRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testExplain.
public void testExplain() throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
ExplainRequest explainRequest = new ExplainRequest(index, id);
explainRequest.query(QueryBuilders.termQuery(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)));
Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
String routing = randomAlphaOfLengthBetween(3, 10);
explainRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
String preference = randomAlphaOfLengthBetween(3, 10);
explainRequest.preference(preference);
expectedParams.put("preference", preference);
}
if (randomBoolean()) {
String[] storedFields = generateRandomStringArray(10, 5, false, false);
String storedFieldsParams = randomFields(storedFields);
explainRequest.storedFields(storedFields);
expectedParams.put("stored_fields", storedFieldsParams);
}
if (randomBoolean()) {
randomizeFetchSourceContextParams(explainRequest::fetchSourceContext, expectedParams);
}
Request request = RequestConverters.explain(explainRequest);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals("/" + index + "/_explain/" + id, request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(explainRequest, request.getEntity());
}
use of org.opensearch.action.explain.ExplainRequest in project OpenSearch by opensearch-project.
the class RequestConverters method explain.
static Request explain(ExplainRequest explainRequest) throws IOException {
String endpoint = endpoint(explainRequest.index(), "_explain", explainRequest.id());
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
Params params = new Params();
params.withStoredFields(explainRequest.storedFields());
params.withFetchSourceContext(explainRequest.fetchSourceContext());
params.withRouting(explainRequest.routing());
params.withPreference(explainRequest.preference());
request.addParameters(params.asMap());
request.setEntity(createEntity(explainRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
Aggregations