Search in sources :

Example 1 with RestClient

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

the class SearchDocumentationIT method testMultiSearchTemplateWithStoredScript.

public void testMultiSearchTemplateWithStoredScript() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    RestClient restClient = client();
    registerQueryScript(restClient);
    // tag::multi-search-template-request-stored
    MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
    String[] searchTerms = { "opensearch", "dashboards" };
    for (String searchTerm : searchTerms) {
        SearchTemplateRequest request = new SearchTemplateRequest();
        request.setRequest(new SearchRequest("posts"));
        request.setScriptType(ScriptType.STORED);
        request.setScript("title_search");
        Map<String, Object> params = new HashMap<>();
        params.put("field", "title");
        params.put("value", searchTerm);
        params.put("size", 5);
        request.setScriptParams(params);
        multiRequest.add(request);
    }
    // end::multi-search-template-request-stored
    // tag::multi-search-template-execute
    MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
    // end::multi-search-template-execute
    assertNotNull(multiResponse);
    assertEquals(searchTerms.length, multiResponse.getResponses().length);
    assertNotNull(multiResponse.getResponses()[0]);
    SearchResponse searchResponse = multiResponse.getResponses()[0].getResponse().getResponse();
    assertTrue(searchResponse.getHits().getTotalHits().value > 0);
    // tag::multi-search-template-execute-listener
    ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::multi-search-template-execute-listener
    // Replace the empty listener by a blocking listener for tests.
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::multi-search-template-execute-async
    client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
    // end::multi-search-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest)

Example 2 with RestClient

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

the class SearchDocumentationIT method testSearchTemplateWithStoredScript.

public void testSearchTemplateWithStoredScript() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    RestClient restClient = client();
    registerQueryScript(restClient);
    // tag::search-template-request-stored
    SearchTemplateRequest request = new SearchTemplateRequest();
    request.setRequest(new SearchRequest("posts"));
    request.setScriptType(ScriptType.STORED);
    request.setScript("title_search");
    Map<String, Object> params = new HashMap<>();
    params.put("field", "title");
    params.put("value", "opensearch");
    params.put("size", 5);
    request.setScriptParams(params);
    // end::search-template-request-stored
    // tag::search-template-request-options
    request.setExplain(true);
    request.setProfile(true);
    // end::search-template-request-options
    // tag::search-template-execute
    SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
    // end::search-template-execute
    SearchResponse searchResponse = response.getResponse();
    assertNotNull(searchResponse);
    assertTrue(searchResponse.getHits().getTotalHits().value > 0);
    // tag::search-template-execute-listener
    ActionListener<SearchTemplateResponse> listener = new ActionListener<SearchTemplateResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::search-template-execute-listener
    // Replace the empty listener by a blocking listener for tests.
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::search-template-execute-async
    // <1>
    client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::search-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse)

Example 3 with RestClient

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

the class SnifferTests method testTaskCancelling.

public void testTaskCancelling() throws Exception {
    RestClient restClient = mock(RestClient.class);
    NodesSniffer nodesSniffer = mock(NodesSniffer.class);
    Scheduler noOpScheduler = new Scheduler() {

        @Override
        public Future<?> schedule(Sniffer.Task task, long delayMillis) {
            return null;
        }

        @Override
        public void shutdown() {
        }
    };
    Sniffer sniffer = new Sniffer(restClient, nodesSniffer, noOpScheduler, 0L, 0L);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    try {
        int numIters = randomIntBetween(50, 100);
        for (int i = 0; i < numIters; i++) {
            Sniffer.Task task = sniffer.new Task(0L);
            TaskWrapper wrapper = new TaskWrapper(task);
            Future<?> future;
            if (rarely()) {
                future = executor.schedule(wrapper, randomLongBetween(0L, 200L), TimeUnit.MILLISECONDS);
            } else {
                future = executor.submit(wrapper);
            }
            Sniffer.ScheduledTask scheduledTask = new Sniffer.ScheduledTask(task, future);
            boolean skip = scheduledTask.skip();
            try {
                assertNull(future.get());
            } catch (CancellationException ignore) {
                assertTrue(future.isCancelled());
            }
            if (skip) {
                // the task was either cancelled before starting, in which case it will never start (thanks to Future#cancel),
                // or skipped, in which case it will run but do nothing (thanks to Task#skip).
                // Here we want to make sure that whenever skip returns true, the task either won't run or it won't do anything,
                // otherwise we may end up with parallel sniffing tracks given that each task schedules the following one. We need to
                // make sure that onFailure takes scheduling over while at the same time ordinary rounds don't go on.
                assertFalse(task.hasStarted());
                assertTrue(task.isSkipped());
                assertTrue(future.isCancelled());
                assertTrue(future.isDone());
            } else {
                // if a future is cancelled when its execution has already started, future#get throws CancellationException before
                // completion. The execution continues though so we use a latch to try and wait for the task to be completed.
                // Here we want to make sure that whenever skip returns false, the task will be completed, otherwise we may be
                // missing to schedule the following round, which means no sniffing will ever happen again besides on failure sniffing.
                assertTrue(wrapper.await());
                // the future may or may not be cancelled but the task has for sure started and completed
                assertTrue(task.toString(), task.hasStarted());
                assertFalse(task.isSkipped());
                assertTrue(future.isDone());
            }
            // subsequent cancel calls return false for sure
            int cancelCalls = randomIntBetween(1, 10);
            for (int j = 0; j < cancelCalls; j++) {
                assertFalse(scheduledTask.skip());
            }
        }
    } finally {
        executor.shutdown();
        executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultScheduler(org.opensearch.client.sniff.Sniffer.DefaultScheduler) Scheduler(org.opensearch.client.sniff.Sniffer.Scheduler) RestClient(org.opensearch.client.RestClient) CancellationException(java.util.concurrent.CancellationException)

Example 4 with RestClient

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

the class OpenSearchNodesSnifferTests method testSniffNodes.

public void testSniffNodes() throws IOException {
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
        try {
            List<Node> sniffedNodes = sniffer.sniff();
            if (sniffResponse.isFailure) {
                fail("sniffNodes should have failed");
            }
            assertEquals(sniffResponse.result, sniffedNodes);
        } catch (ResponseException e) {
            Response response = e.getResponse();
            if (sniffResponse.isFailure) {
                final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
                assertThat(e.getMessage(), startsWith(errorPrefix));
                assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
                assertThat(response.getHost(), equalTo(httpHost));
                assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
                assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
            } else {
                fail("sniffNodes should have succeeded: " + response.getStatusLine());
            }
        }
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) HttpHost(org.apache.http.HttpHost) Node(org.opensearch.client.Node) RestClient(org.opensearch.client.RestClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 5 with RestClient

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

the class SnifferBuilderTests method testBuild.

public void testBuild() throws Exception {
    int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
    HttpHost[] hosts = new HttpHost[numNodes];
    for (int i = 0; i < numNodes; i++) {
        hosts[i] = new HttpHost("localhost", 9200 + i);
    }
    try (RestClient client = RestClient.builder(hosts).build()) {
        try {
            Sniffer.builder(null).build();
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("restClient cannot be null", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
            fail("should have failed");
        } catch (IllegalArgumentException e) {
            assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage());
        }
        try {
            Sniffer.builder(client).setNodesSniffer(null);
            fail("should have failed");
        } catch (NullPointerException e) {
            assertEquals("nodesSniffer cannot be null", e.getMessage());
        }
        try (Sniffer sniffer = Sniffer.builder(client).build()) {
            assertNotNull(sniffer);
        }
        SnifferBuilder builder = Sniffer.builder(client);
        if (getRandom().nextBoolean()) {
            builder.setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
        }
        if (getRandom().nextBoolean()) {
            builder.setNodesSniffer(new MockNodesSniffer());
        }
        try (Sniffer sniffer = builder.build()) {
            assertNotNull(sniffer);
        }
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.opensearch.client.RestClient)

Aggregations

RestClient (org.opensearch.client.RestClient)38 Request (org.opensearch.client.Request)17 HttpHost (org.apache.http.HttpHost)13 Response (org.opensearch.client.Response)13 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 DefaultScheduler (org.opensearch.client.sniff.Sniffer.DefaultScheduler)7 Scheduler (org.opensearch.client.sniff.Sniffer.Scheduler)7 Settings (org.opensearch.common.settings.Settings)7 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Node (org.opensearch.client.Node)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Environment (org.opensearch.env.Environment)4 TestEnvironment (org.opensearch.env.TestEnvironment)4 ResourceWatcherService (org.opensearch.watcher.ResourceWatcherService)4 Path (java.nio.file.Path)3