Search in sources :

Example 46 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testCount.

@SuppressWarnings({ "unused", "unchecked" })
public void testCount() throws Exception {
    indexCountTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::count-request-basic
        // <1>
        CountRequest countRequest = new CountRequest();
        // <2>
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // <3>
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // <4>
        countRequest.source(searchSourceBuilder);
    // end::count-request-basic
    }
    {
        // tag::count-request-args
        CountRequest countRequest = // <1>
        new CountRequest("blog").routing(// <2>
        "routing").indicesOptions(// <3>
        IndicesOptions.lenientExpandOpen()).preference(// <4>
        "_local");
        // end::count-request-args
        assertNotNull(client.count(countRequest, RequestOptions.DEFAULT));
    }
    {
        // tag::count-source-basics
        // <1>
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // <2>
        sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
        // end::count-source-basics
        // tag::count-source-setter
        CountRequest countRequest = new CountRequest();
        countRequest.indices("blog", "author");
        countRequest.source(sourceBuilder);
        // end::count-source-setter
        // tag::count-execute
        CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
        // end::count-execute
        // tag::count-execute-listener
        ActionListener<CountResponse> listener = new ActionListener<CountResponse>() {

            @Override
            public void onResponse(CountResponse countResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::count-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::count-execute-async
        // <1>
        client.countAsync(countRequest, RequestOptions.DEFAULT, listener);
        // end::count-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
        // tag::count-response-1
        long count = countResponse.getCount();
        RestStatus status = countResponse.status();
        Boolean terminatedEarly = countResponse.isTerminatedEarly();
        // end::count-response-1
        // tag::count-response-2
        int totalShards = countResponse.getTotalShards();
        int skippedShards = countResponse.getSkippedShards();
        int successfulShards = countResponse.getSuccessfulShards();
        int failedShards = countResponse.getFailedShards();
        for (ShardSearchFailure failure : countResponse.getShardFailures()) {
        // failures should be handled here
        }
        // end::count-response-2
        assertNotNull(countResponse);
        assertEquals(4, countResponse.getCount());
    }
}
Also used : CountResponse(org.opensearch.client.core.CountResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CountRequest(org.opensearch.client.core.CountRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RestStatus(org.opensearch.rest.RestStatus) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 47 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class StoredScriptsDocumentationIT method testGetStoredScript.

@SuppressWarnings("unused")
public void testGetStoredScript() throws Exception {
    RestHighLevelClient client = highLevelClient();
    final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
    putStoredScript("calculate-score", scriptSource);
    {
        // tag::get-stored-script-request
        // <1>
        GetStoredScriptRequest request = new GetStoredScriptRequest("calculate-score");
        // end::get-stored-script-request
        // tag::get-stored-script-request-masterTimeout
        // <1>
        request.masterNodeTimeout(TimeValue.timeValueSeconds(50));
        // <2>
        request.masterNodeTimeout("50s");
        // end::get-stored-script-request-masterTimeout
        // tag::get-stored-script-execute
        GetStoredScriptResponse getResponse = client.getScript(request, RequestOptions.DEFAULT);
        // end::get-stored-script-execute
        // tag::get-stored-script-response
        // <1>
        StoredScriptSource storedScriptSource = getResponse.getSource();
        // <2>
        String lang = storedScriptSource.getLang();
        // <3>
        String source = storedScriptSource.getSource();
        // <4>
        Map<String, String> options = storedScriptSource.getOptions();
        // end::get-stored-script-response
        assertThat(storedScriptSource, equalTo(scriptSource));
        // tag::get-stored-script-execute-listener
        ActionListener<GetStoredScriptResponse> listener = new ActionListener<GetStoredScriptResponse>() {

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

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-stored-script-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-stored-script-execute-async
        // <1>
        client.getScriptAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-stored-script-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) IOException(java.io.IOException) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 48 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class StoredScriptsDocumentationIT method testDeleteStoredScript.

@SuppressWarnings("unused")
public void testDeleteStoredScript() throws Exception {
    RestHighLevelClient client = highLevelClient();
    final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
    putStoredScript("calculate-score", scriptSource);
    // tag::delete-stored-script-request
    // <1>
    DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest("calculate-score");
    // end::delete-stored-script-request
    // tag::delete-stored-script-request-masterTimeout
    // <1>
    deleteRequest.masterNodeTimeout(TimeValue.timeValueSeconds(50));
    // <2>
    deleteRequest.masterNodeTimeout("50s");
    // end::delete-stored-script-request-masterTimeout
    // tag::delete-stored-script-request-timeout
    // <1>
    deleteRequest.timeout(TimeValue.timeValueSeconds(60));
    // <2>
    deleteRequest.timeout("60s");
    // end::delete-stored-script-request-timeout
    // tag::delete-stored-script-execute
    AcknowledgedResponse deleteResponse = client.deleteScript(deleteRequest, RequestOptions.DEFAULT);
    // end::delete-stored-script-execute
    // tag::delete-stored-script-response
    // <1>
    boolean acknowledged = deleteResponse.isAcknowledged();
    // end::delete-stored-script-response
    putStoredScript("calculate-score", scriptSource);
    // tag::delete-stored-script-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::delete-stored-script-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::delete-stored-script-execute-async
    // <1>
    client.deleteScriptAsync(deleteRequest, RequestOptions.DEFAULT, listener);
    // end::delete-stored-script-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) StoredScriptSource(org.opensearch.script.StoredScriptSource) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 49 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testGetSettings.

public void testGetSettings() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Settings settings = Settings.builder().put("number_of_shards", 3).build();
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    // tag::get-settings-request
    // <1>
    GetSettingsRequest request = new GetSettingsRequest().indices("index");
    // end::get-settings-request
    // tag::get-settings-request-names
    // <1>
    request.names("index.number_of_shards");
    // end::get-settings-request-names
    // tag::get-settings-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::get-settings-request-indicesOptions
    // tag::get-settings-execute
    GetSettingsResponse getSettingsResponse = client.indices().getSettings(request, RequestOptions.DEFAULT);
    // end::get-settings-execute
    // tag::get-settings-response
    // <1>
    String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
    // <2>
    Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
    // <3>
    Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
    // end::get-settings-response
    assertEquals("3", numberOfShardsString);
    assertEquals(Integer.valueOf(3), numberOfShards);
    assertNull("refresh_interval returned but was never set!", getSettingsResponse.getSetting("index", "index.refresh_interval"));
    // tag::get-settings-execute-listener
    ActionListener<GetSettingsResponse> listener = new ActionListener<GetSettingsResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-settings-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-settings-execute-async
    // <1>
    client.indices().getSettingsAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-settings-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 50 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testGetTemplates.

public void testGetTemplates() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
        putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
        putRequest.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1));
        putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON);
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::get-templates-request
    // <1>
    GetIndexTemplatesRequest request = new GetIndexTemplatesRequest("my-template");
    // <2>
    request = new GetIndexTemplatesRequest("template-1", "template-2");
    // <3>
    request = new GetIndexTemplatesRequest("my-*");
    // end::get-templates-request
    // tag::get-templates-request-masterTimeout
    // <1>
    request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.setMasterNodeTimeout("1m");
    // end::get-templates-request-masterTimeout
    // tag::get-templates-execute
    GetIndexTemplatesResponse getTemplatesResponse = client.indices().getIndexTemplate(request, RequestOptions.DEFAULT);
    // end::get-templates-execute
    // tag::get-templates-response
    // <1>
    List<IndexTemplateMetadata> templates = getTemplatesResponse.getIndexTemplates();
    // end::get-templates-response
    assertThat(templates, hasSize(1));
    assertThat(templates.get(0).name(), equalTo("my-template"));
    // tag::get-templates-execute-listener
    ActionListener<GetIndexTemplatesResponse> listener = new ActionListener<GetIndexTemplatesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-templates-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-templates-execute-async
    // <1>
    client.indices().getIndexTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-templates-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetIndexTemplatesResponse(org.opensearch.client.indices.GetIndexTemplatesResponse) IndexTemplateMetadata(org.opensearch.client.indices.IndexTemplateMetadata) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)63 LatchedActionListener (org.opensearch.action.LatchedActionListener)63 ActionListener (org.opensearch.action.ActionListener)57 IOException (java.io.IOException)43 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)38 OpenSearchException (org.opensearch.OpenSearchException)24 HashMap (java.util.HashMap)20 Map (java.util.Map)19 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)18 ArrayList (java.util.ArrayList)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.opensearch.common.settings.Settings)15 TestThreadPool (org.opensearch.threadpool.TestThreadPool)14 List (java.util.List)12 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)11 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)11 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)10 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)10