Search in sources :

Example 76 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class OpenSearchRestTestCase method ensureHealth.

protected static void ensureHealth(RestClient client, String index, Consumer<Request> requestConsumer) throws IOException {
    Request request = new Request("GET", "/_cluster/health" + (index.trim().isEmpty() ? "" : "/" + index));
    requestConsumer.accept(request);
    try {
        client.performRequest(request);
    } catch (ResponseException e) {
        if (e.getResponse().getStatusLine().getStatusCode() == HttpStatus.SC_REQUEST_TIMEOUT) {
            try {
                final Response clusterStateResponse = client.performRequest(new Request("GET", "/_cluster/state?pretty"));
                fail("timed out waiting for green state for index [" + index + "] " + "cluster state [" + EntityUtils.toString(clusterStateResponse.getEntity()) + "]");
            } catch (Exception inner) {
                e.addSuppressed(inner);
            }
        }
        throw e;
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ResponseException(org.opensearch.client.ResponseException)

Example 77 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class ClientYamlTestClient method callApi.

/**
 * Calls an api with the provided parameters and body
 */
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, HttpEntity entity, Map<String, String> headers, NodeSelector nodeSelector) throws IOException {
    ClientYamlSuiteRestApi restApi = restApi(apiName);
    Set<String> apiRequiredParameters = restApi.getParams().entrySet().stream().filter(Entry::getValue).map(Entry::getKey).collect(Collectors.toSet());
    List<ClientYamlSuiteRestApi.Path> bestPaths = restApi.getBestMatchingPaths(params.keySet());
    // the rest path to use is randomized out of the matching ones (if more than one)
    ClientYamlSuiteRestApi.Path path = RandomizedTest.randomFrom(bestPaths);
    // divide params between ones that go within query string and ones that go within path
    Map<String, String> pathParts = new HashMap<>();
    Map<String, String> queryStringParams = new HashMap<>();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (path.getParts().contains(entry.getKey())) {
            pathParts.put(entry.getKey(), entry.getValue());
        } else if (restApi.getParams().containsKey(entry.getKey()) || restSpec.isGlobalParameter(entry.getKey()) || restSpec.isClientParameter(entry.getKey())) {
            queryStringParams.put(entry.getKey(), entry.getValue());
            apiRequiredParameters.remove(entry.getKey());
        } else {
            throw new IllegalArgumentException("path/param [" + entry.getKey() + "] not supported by [" + restApi.getName() + "] " + "api");
        }
    }
    if (false == apiRequiredParameters.isEmpty()) {
        throw new IllegalArgumentException("missing required parameter: " + apiRequiredParameters + " by [" + restApi.getName() + "] api");
    }
    Set<String> partNames = pathParts.keySet();
    if (path.getParts().size() != partNames.size() || path.getParts().containsAll(partNames) == false) {
        throw new IllegalStateException("provided path parts don't match the best matching path: " + path.getParts() + " - " + partNames);
    }
    String finalPath = path.getPath();
    for (Entry<String, String> pathPart : pathParts.entrySet()) {
        try {
            // Encode rules for path and query string parameters are different. We use URI to encode the path. We need to encode each
            // path part separately, as each one might contain slashes that need to be escaped, which needs to be done manually.
            // We prepend "/" to the path part to handle parts that start with - or other invalid characters.
            URI uri = new URI(null, null, null, -1, "/" + pathPart.getValue(), null, null);
            // manually escape any slash that each part may contain
            String encodedPathPart = uri.getRawPath().substring(1).replaceAll("/", "%2F");
            finalPath = finalPath.replace("{" + pathPart.getKey() + "}", encodedPathPart);
        } catch (URISyntaxException e) {
            throw new RuntimeException("unable to build uri", e);
        }
    }
    List<String> supportedMethods = Arrays.asList(path.getMethods());
    String requestMethod;
    if (entity != null) {
        if (false == restApi.isBodySupported()) {
            throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api");
        }
        String contentType = entity.getContentType().getValue();
        // randomly test the GET with source param instead of GET/POST with body
        if (sendBodyAsSourceParam(supportedMethods, contentType, entity.getContentLength())) {
            logger.debug("sending the request body as source param with GET method");
            queryStringParams.put("source", EntityUtils.toString(entity));
            queryStringParams.put("source_content_type", contentType);
            requestMethod = HttpGet.METHOD_NAME;
            entity = null;
        } else {
            requestMethod = RandomizedTest.randomFrom(supportedMethods);
        }
    } else {
        if (restApi.isBodyRequired()) {
            throw new IllegalArgumentException("body is required by [" + restApi.getName() + "] api");
        }
        requestMethod = RandomizedTest.randomFrom(supportedMethods);
    }
    logger.debug("calling api [{}]", apiName);
    Request request = new Request(requestMethod, finalPath);
    for (Map.Entry<String, String> param : queryStringParams.entrySet()) {
        request.addParameter(param.getKey(), param.getValue());
    }
    request.setEntity(entity);
    setOptions(request, headers);
    try {
        Response response = getRestClient(nodeSelector).performRequest(request);
        return new ClientYamlTestResponse(response);
    } catch (ResponseException e) {
        throw new ClientYamlTestResponseException(e);
    }
}
Also used : HashMap(java.util.HashMap) ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Response(org.opensearch.client.Response) ClientYamlSuiteRestApi(org.opensearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 78 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testGetSource.

public void testGetSource() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Request createIndex = new Request("PUT", "/posts");
        createIndex.setJsonEntity("{\n" + "    \"mappings\" : {\n" + "        \"properties\" : {\n" + "            \"message\" : {\n" + "                \"type\": \"text\",\n" + "                \"store\": true\n" + "            }\n" + "        }\n" + "    }\n" + "}");
        Response response = client().performRequest(createIndex);
        assertEquals(200, response.getStatusLine().getStatusCode());
        IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "foobar", "postDate", new Date(), "message", "trying out OpenSearch");
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
    }
    // tag::get-source-request
    GetSourceRequest getSourceRequest = new GetSourceRequest(// <1>
    "posts", // <2>
    "1");
    // end::get-source-request
    // tag::get-source-request-optional
    // <2>
    String[] includes = Strings.EMPTY_ARRAY;
    String[] excludes = new String[] { "postDate" };
    getSourceRequest.fetchSourceContext(// <1>
    new FetchSourceContext(true, includes, excludes));
    // end::get-source-request-optional
    // tag::get-source-request-routing
    // <1>
    getSourceRequest.routing("routing");
    // end::get-source-request-routing
    // tag::get-source-request-preference
    // <1>
    getSourceRequest.preference("preference");
    // end::get-source-request-preference
    // tag::get-source-request-realtime
    // <1>
    getSourceRequest.realtime(false);
    // end::get-source-request-realtime
    // tag::get-source-request-refresh
    // <1>
    getSourceRequest.refresh(true);
    // end::get-source-request-refresh
    {
        // tag::get-source-execute
        GetSourceResponse response = client.getSource(getSourceRequest, RequestOptions.DEFAULT);
        // end::get-source-execute
        // tag::get-source-response
        Map<String, Object> source = response.getSource();
        // end::get-source-response
        Map<String, Object> expectSource = new HashMap<>();
        expectSource.put("user", "foobar");
        expectSource.put("message", "trying out OpenSearch");
        assertEquals(expectSource, source);
    }
    {
        GetSourceRequest request = new GetSourceRequest("posts", "1");
        // tag::get-source-execute-listener
        ActionListener<GetSourceResponse> listener = new ActionListener<GetSourceResponse>() {

            @Override
            public void onResponse(GetSourceResponse getResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-source-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::get-source-execute-async
        // <1>
        client.getSourceAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-source-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) OpenSearchException(org.opensearch.OpenSearchException) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) GetSourceResponse(org.opensearch.client.core.GetSourceResponse)

Example 79 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testTermVectors.

// Not entirely sure if _termvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testTermVectors() throws Exception {
    RestHighLevelClient client = highLevelClient();
    CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("user").field("type", "keyword").endObject().endObject().endObject());
    CreateIndexResponse authorsResponse = client.indices().create(authorsRequest, RequestOptions.DEFAULT);
    assertTrue(authorsResponse.isAcknowledged());
    client.index(new IndexRequest("index").id("1").source("user", "foobar"), RequestOptions.DEFAULT);
    Response refreshResponse = client().performRequest(new Request("POST", "/authors/_refresh"));
    assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
    {
        // tag::term-vectors-request
        TermVectorsRequest request = new TermVectorsRequest("authors", "1");
        request.setFields("user");
    // end::term-vectors-request
    }
    {
        // tag::term-vectors-request-artificial
        XContentBuilder docBuilder = XContentFactory.jsonBuilder();
        docBuilder.startObject().field("user", "guest-user").endObject();
        TermVectorsRequest request = new TermVectorsRequest("authors", // <1>
        docBuilder);
        // end::term-vectors-request-artificial
        // tag::term-vectors-request-optional-arguments
        // <1>
        request.setFieldStatistics(false);
        // <2>
        request.setTermStatistics(true);
        // <3>
        request.setPositions(false);
        // <4>
        request.setOffsets(false);
        // <5>
        request.setPayloads(false);
        Map<String, Integer> filterSettings = new HashMap<>();
        filterSettings.put("max_num_terms", 3);
        filterSettings.put("min_term_freq", 1);
        filterSettings.put("max_term_freq", 10);
        filterSettings.put("min_doc_freq", 1);
        filterSettings.put("max_doc_freq", 100);
        filterSettings.put("min_word_length", 1);
        filterSettings.put("max_word_length", 10);
        // <6>
        request.setFilterSettings(filterSettings);
        Map<String, String> perFieldAnalyzer = new HashMap<>();
        perFieldAnalyzer.put("user", "keyword");
        // <7>
        request.setPerFieldAnalyzer(perFieldAnalyzer);
        // <8>
        request.setRealtime(false);
        // <9>
        request.setRouting("routing");
    // end::term-vectors-request-optional-arguments
    }
    TermVectorsRequest request = new TermVectorsRequest("authors", "1");
    request.setFields("user");
    // tag::term-vectors-execute
    TermVectorsResponse response = client.termvectors(request, RequestOptions.DEFAULT);
    // end::term-vectors-execute
    // tag::term-vectors-response
    // <1>
    String index = response.getIndex();
    // <2>
    String id = response.getId();
    // <3>
    boolean found = response.getFound();
    if (response.getTermVectorsList() != null) {
        // tag::term-vectors-term-vectors
        for (TermVectorsResponse.TermVector tv : response.getTermVectorsList()) {
            // <1>
            String fieldname = tv.getFieldName();
            // <2>
            int docCount = tv.getFieldStatistics().getDocCount();
            long sumTotalTermFreq = // <3>
            tv.getFieldStatistics().getSumTotalTermFreq();
            // <4>
            long sumDocFreq = tv.getFieldStatistics().getSumDocFreq();
            if (tv.getTerms() != null) {
                List<TermVectorsResponse.TermVector.Term> terms = // <5>
                tv.getTerms();
                for (TermVectorsResponse.TermVector.Term term : terms) {
                    // <6>
                    String termStr = term.getTerm();
                    // <7>
                    int termFreq = term.getTermFreq();
                    // <8>
                    int docFreq = term.getDocFreq();
                    // <9>
                    long totalTermFreq = term.getTotalTermFreq();
                    // <10>
                    float score = term.getScore();
                    if (term.getTokens() != null) {
                        List<TermVectorsResponse.TermVector.Token> tokens = // <11>
                        term.getTokens();
                        for (TermVectorsResponse.TermVector.Token token : tokens) {
                            // <12>
                            int position = token.getPosition();
                            // <13>
                            int startOffset = token.getStartOffset();
                            // <14>
                            int endOffset = token.getEndOffset();
                            // <15>
                            String payload = token.getPayload();
                        }
                    }
                }
            }
        }
    // end::term-vectors-term-vectors
    }
    ActionListener<TermVectorsResponse> listener;
    // tag::term-vectors-execute-listener
    listener = new ActionListener<TermVectorsResponse>() {

        @Override
        public void onResponse(TermVectorsResponse termVectorsResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::term-vectors-execute-listener
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::term-vectors-execute-async
    // <1>
    client.termvectorsAsync(request, RequestOptions.DEFAULT, listener);
    // end::term-vectors-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchException(org.opensearch.OpenSearchException) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 80 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testMultiGet.

@SuppressWarnings("unused")
public void testMultiGet() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Request createIndex = new Request("PUT", "/index");
        createIndex.setJsonEntity("{\n" + "    \"mappings\" : {\n" + "        \"properties\" : {\n" + "            \"foo\" : {\n" + "                \"type\": \"text\",\n" + "                \"store\": true\n" + "            }\n" + "        }\n" + "    }\n" + "}");
        Response response = client().performRequest(createIndex);
        assertEquals(200, response.getStatusLine().getStatusCode());
    }
    Map<String, Object> source = new HashMap<>();
    source.put("foo", "val1");
    source.put("bar", "val2");
    source.put("baz", "val3");
    client.index(new IndexRequest("index").id("example_id").source(source).setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
    {
        // tag::multi-get-request
        MultiGetRequest request = new MultiGetRequest();
        request.add(new MultiGetRequest.Item(// <1>
        "index", // <2>
        "example_id"));
        // <3>
        request.add(new MultiGetRequest.Item("index", "another_id"));
        // end::multi-get-request
        // Add a missing index so we can test it.
        request.add(new MultiGetRequest.Item("missing_index", "id"));
        // tag::multi-get-request-item-extras
        request.add(new MultiGetRequest.Item("index", "with_routing").routing(// <1>
        "some_routing"));
        request.add(new MultiGetRequest.Item("index", "with_version").versionType(// <2>
        VersionType.EXTERNAL).version(// <3>
        10123L));
        // end::multi-get-request-item-extras
        // tag::multi-get-request-top-level-extras
        // <1>
        request.preference("some_preference");
        // <2>
        request.realtime(false);
        // <3>
        request.refresh(true);
        // end::multi-get-request-top-level-extras
        // tag::multi-get-execute
        MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
        // end::multi-get-execute
        // tag::multi-get-response
        MultiGetItemResponse firstItem = response.getResponses()[0];
        // <1>
        assertNull(firstItem.getFailure());
        // <2>
        GetResponse firstGet = firstItem.getResponse();
        String index = firstItem.getIndex();
        String id = firstItem.getId();
        if (firstGet.isExists()) {
            long version = firstGet.getVersion();
            // <3>
            String sourceAsString = firstGet.getSourceAsString();
            // <4>
            Map<String, Object> sourceAsMap = firstGet.getSourceAsMap();
            // <5>
            byte[] sourceAsBytes = firstGet.getSourceAsBytes();
        } else {
        // <6>
        }
        // end::multi-get-response
        assertTrue(firstGet.isExists());
        assertEquals(source, firstGet.getSource());
        MultiGetItemResponse missingIndexItem = response.getResponses()[2];
        // tag::multi-get-indexnotfound
        // <1>
        assertNull(missingIndexItem.getResponse());
        // <2>
        Exception e = missingIndexItem.getFailure().getFailure();
        // <3>
        OpenSearchException ee = (OpenSearchException) e;
        // TODO status is broken! fix in a followup
        // assertEquals(RestStatus.NOT_FOUND, ee.status());        // <4>
        assertThat(e.getMessage(), // <5>
        containsString("reason=no such index [missing_index]"));
        // end::multi-get-indexnotfound
        ActionListener<MultiGetResponse> listener;
        // tag::multi-get-execute-listener
        listener = new ActionListener<MultiGetResponse>() {

            @Override
            public void onResponse(MultiGetResponse response) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::multi-get-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::multi-get-execute-async
        // <1>
        client.mgetAsync(request, RequestOptions.DEFAULT, listener);
        // end::multi-get-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
    {
        MultiGetRequest request = new MultiGetRequest();
        // tag::multi-get-request-no-source
        request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
        FetchSourceContext.DO_NOT_FETCH_SOURCE));
        // end::multi-get-request-no-source
        MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
        assertNull(item.getResponse().getSource());
    }
    {
        MultiGetRequest request = new MultiGetRequest();
        // tag::multi-get-request-source-include
        String[] includes = new String[] { "foo", "*r" };
        String[] excludes = Strings.EMPTY_ARRAY;
        FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
        request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
        fetchSourceContext));
        // end::multi-get-request-source-include
        MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
        assertThat(item.getResponse().getSource(), hasEntry("foo", "val1"));
        assertThat(item.getResponse().getSource(), hasEntry("bar", "val2"));
        assertThat(item.getResponse().getSource(), not(hasKey("baz")));
    }
    {
        MultiGetRequest request = new MultiGetRequest();
        // tag::multi-get-request-source-exclude
        String[] includes = Strings.EMPTY_ARRAY;
        String[] excludes = new String[] { "foo", "*r" };
        FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
        request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
        fetchSourceContext));
        // end::multi-get-request-source-exclude
        MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
        assertThat(item.getResponse().getSource(), not(hasKey("foo")));
        assertThat(item.getResponse().getSource(), not(hasKey("bar")));
        assertThat(item.getResponse().getSource(), hasEntry("baz", "val3"));
    }
    {
        MultiGetRequest request = new MultiGetRequest();
        // tag::multi-get-request-stored
        request.add(new MultiGetRequest.Item("index", "example_id").storedFields(// <1>
        "foo"));
        MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
        MultiGetItemResponse item = response.getResponses()[0];
        // <2>
        String value = item.getResponse().getField("foo").getValue();
        // end::multi-get-request-stored
        assertNull(item.getResponse().getSource());
        assertEquals("val1", value);
    }
    {
        // tag::multi-get-conflict
        MultiGetRequest request = new MultiGetRequest();
        request.add(new MultiGetRequest.Item("index", "example_id").version(1000L));
        MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
        MultiGetItemResponse item = response.getResponses()[0];
        // <1>
        assertNull(item.getResponse());
        // <2>
        Exception e = item.getFailure().getFailure();
        // <3>
        OpenSearchException ee = (OpenSearchException) e;
        // TODO status is broken! fix in a followup
        // assertEquals(RestStatus.CONFLICT, ee.status());          // <4>
        assertThat(e.getMessage(), containsString("version conflict, current version [1] is " + // <5>
        "different than the one provided [1000]"));
    // end::multi-get-conflict
    }
}
Also used : MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) HashMap(java.util.HashMap) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) GetResponse(org.opensearch.action.get.GetResponse) OpenSearchException(org.opensearch.OpenSearchException) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) OpenSearchException(org.opensearch.OpenSearchException) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7