Search in sources :

Example 1 with ExplainRequest

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));
}
Also used : GetResult(org.opensearch.index.get.GetResult) DocumentField(org.opensearch.common.document.DocumentField) Explanation(org.apache.lucene.search.Explanation) ExplainResponse(org.opensearch.action.explain.ExplainResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) ExplainRequest(org.opensearch.action.explain.ExplainRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 2 with ExplainRequest

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);
}
Also used : ExplainRequest(org.opensearch.action.explain.ExplainRequest)

Example 3 with ExplainRequest

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());
}
Also used : ExplainResponse(org.opensearch.action.explain.ExplainResponse) ExplainRequest(org.opensearch.action.explain.ExplainRequest)

Example 4 with ExplainRequest

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());
}
Also used : HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest)

Example 5 with ExplainRequest

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;
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Aggregations

ExplainRequest (org.opensearch.action.explain.ExplainRequest)6 IOException (java.io.IOException)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)2 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 DeleteRequest (org.opensearch.action.delete.DeleteRequest)2 ExplainResponse (org.opensearch.action.explain.ExplainResponse)2 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)2 GetRequest (org.opensearch.action.get.GetRequest)2 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)2 WriteRequest (org.opensearch.action.support.WriteRequest)2 UpdateRequest (org.opensearch.action.update.UpdateRequest)2 CountRequest (org.opensearch.client.core.CountRequest)2