Search in sources :

Example 51 with LatchedActionListener

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

the class IndicesClientDocumentationIT method testSplitIndex.

public void testSplitIndex() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        createIndex("source_index", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).put("index.number_of_routing_shards", 4).build());
        updateIndexSettings("source_index", Settings.builder().put("index.blocks.write", true));
    }
    // tag::split-index-request
    // <1>
    ResizeRequest request = new ResizeRequest("target_index", "source_index");
    // <2>
    request.setResizeType(ResizeType.SPLIT);
    // end::split-index-request
    // tag::split-index-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(2));
    // <2>
    request.timeout("2m");
    // end::split-index-request-timeout
    // tag::split-index-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::split-index-request-masterTimeout
    // tag::split-index-request-waitForActiveShards
    // <1>
    request.setWaitForActiveShards(2);
    // <2>
    request.setWaitForActiveShards(ActiveShardCount.DEFAULT);
    // end::split-index-request-waitForActiveShards
    // tag::split-index-request-settings
    request.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
    4));
    // end::split-index-request-settings
    // tag::split-index-request-aliases
    // <1>
    request.getTargetIndexRequest().alias(new Alias("target_alias"));
    // end::split-index-request-aliases
    // tag::split-index-execute
    ResizeResponse resizeResponse = client.indices().split(request, RequestOptions.DEFAULT);
    // end::split-index-execute
    // tag::split-index-response
    // <1>
    boolean acknowledged = resizeResponse.isAcknowledged();
    // <2>
    boolean shardsAcked = resizeResponse.isShardsAcknowledged();
    // end::split-index-response
    assertTrue(acknowledged);
    assertTrue(shardsAcked);
    // tag::split-index-execute-listener
    ActionListener<ResizeResponse> listener = new ActionListener<ResizeResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::split-index-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::split-index-execute-async
    // <1>
    client.indices().splitAsync(request, RequestOptions.DEFAULT, listener);
    // end::split-index-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) Alias(org.opensearch.action.admin.indices.alias.Alias) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

Example 52 with LatchedActionListener

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

the class IndicesClientDocumentationIT method testAnalyze.

public void testAnalyze() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::analyze-builtin-request
        AnalyzeRequest request = // <1>
        AnalyzeRequest.withGlobalAnalyzer(// <1>
        "english", "Some text to analyze", // <2>
        "Some more text to analyze");
    // end::analyze-builtin-request
    }
    {
        // tag::analyze-custom-request
        Map<String, Object> stopFilter = new HashMap<>();
        stopFilter.put("type", "stop");
        // <1>
        stopFilter.put("stopwords", new String[] { "to" });
        AnalyzeRequest request = // <2>
        AnalyzeRequest.buildCustomAnalyzer("standard").addCharFilter(// <3>
        "html_strip").addTokenFilter(// <4>
        "lowercase").addTokenFilter(// <5>
        stopFilter).build("<b>Some text to analyze</b>");
    // end::analyze-custom-request
    }
    {
        // tag::analyze-custom-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.buildCustomNormalizer().addTokenFilter("lowercase").build("<b>BaR</b>");
        // end::analyze-custom-normalizer-request
        // tag::analyze-request-explain
        // <1>
        request.explain(true);
        // <2>
        request.attributes("keyword", "type");
        // end::analyze-request-explain
        // tag::analyze-execute
        AnalyzeResponse response = client.indices().analyze(request, RequestOptions.DEFAULT);
        // end::analyze-execute
        // tag::analyze-response-tokens
        // <1>
        List<AnalyzeResponse.AnalyzeToken> tokens = response.getTokens();
        // end::analyze-response-tokens
        // tag::analyze-response-detail
        // <1>
        DetailAnalyzeResponse detail = response.detail();
        // end::analyze-response-detail
        assertNull(tokens);
        assertNotNull(detail.tokenizer());
    }
    CreateIndexRequest req = new CreateIndexRequest("my_index");
    CreateIndexResponse resp = client.indices().create(req, RequestOptions.DEFAULT);
    assertTrue(resp.isAcknowledged());
    PutMappingRequest pmReq = new PutMappingRequest("my_index").source(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("my_field").field("type", "text").field("analyzer", "english").endObject().endObject().endObject());
    AcknowledgedResponse pmResp = client.indices().putMapping(pmReq, RequestOptions.DEFAULT);
    assertTrue(pmResp.isAcknowledged());
    {
        // tag::analyze-index-request
        AnalyzeRequest request = AnalyzeRequest.withIndexAnalyzer(// <1>
        "my_index", // <2>
        "my_analyzer", "some text to analyze");
        // end::analyze-index-request
        // tag::analyze-execute-listener
        ActionListener<AnalyzeResponse> listener = new ActionListener<AnalyzeResponse>() {

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

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::analyze-execute-listener
        // use a built-in analyzer in the test
        request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
        // Use a blocking listener in the test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::analyze-execute-async
        // <1>
        client.indices().analyzeAsync(request, RequestOptions.DEFAULT, listener);
        // end::analyze-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
    {
        // tag::analyze-index-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.withNormalizer(// <1>
        "my_index", // <2>
        "my_normalizer", "some text to analyze");
    // end::analyze-index-normalizer-request
    }
    {
        // tag::analyze-field-request
        AnalyzeRequest request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
    // end::analyze-field-request
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) AnalyzeResponse(org.opensearch.client.indices.AnalyzeResponse) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) List(java.util.List) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 53 with LatchedActionListener

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

the class IndicesClientDocumentationIT method testShrinkIndex.

public void testShrinkIndex() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Map<String, Object> nodes = getAsMap("_nodes");
        @SuppressWarnings("unchecked") String firstNode = ((Map<String, Object>) nodes.get("nodes")).keySet().iterator().next();
        createIndex("source_index", Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build());
        updateIndexSettings("source_index", Settings.builder().put("index.routing.allocation.require._name", firstNode).put("index.blocks.write", true));
    }
    // tag::shrink-index-request
    // <1>
    ResizeRequest request = new ResizeRequest("target_index", "source_index");
    // end::shrink-index-request
    // tag::shrink-index-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(2));
    // <2>
    request.timeout("2m");
    // end::shrink-index-request-timeout
    // tag::shrink-index-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::shrink-index-request-masterTimeout
    // tag::shrink-index-request-waitForActiveShards
    // <1>
    request.setWaitForActiveShards(2);
    // <2>
    request.setWaitForActiveShards(ActiveShardCount.DEFAULT);
    // end::shrink-index-request-waitForActiveShards
    // tag::shrink-index-request-settings
    request.getTargetIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
    2).putNull(// <2>
    "index.routing.allocation.require._name"));
    // end::shrink-index-request-settings
    // tag::shrink-index-request-aliases
    // <1>
    request.getTargetIndexRequest().alias(new Alias("target_alias"));
    // end::shrink-index-request-aliases
    // tag::shrink-index-execute
    ResizeResponse resizeResponse = client.indices().shrink(request, RequestOptions.DEFAULT);
    // end::shrink-index-execute
    // tag::shrink-index-response
    // <1>
    boolean acknowledged = resizeResponse.isAcknowledged();
    // <2>
    boolean shardsAcked = resizeResponse.isShardsAcknowledged();
    // end::shrink-index-response
    assertTrue(acknowledged);
    assertTrue(shardsAcked);
    // tag::shrink-index-execute-listener
    ActionListener<ResizeResponse> listener = new ActionListener<ResizeResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::shrink-index-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::shrink-index-execute-async
    // <1>
    client.indices().shrinkAsync(request, RequestOptions.DEFAULT, listener);
    // end::shrink-index-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) Alias(org.opensearch.action.admin.indices.alias.Alias) Map(java.util.Map) HashMap(java.util.HashMap) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest)

Example 54 with LatchedActionListener

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

the class ClusterClientDocumentationIT method testClusterGetSettingsAsync.

public void testClusterGetSettingsAsync() throws InterruptedException {
    RestHighLevelClient client = highLevelClient();
    ClusterGetSettingsRequest request = new ClusterGetSettingsRequest();
    // tag::get-settings-execute-listener
    ActionListener<ClusterGetSettingsResponse> listener = new ActionListener<ClusterGetSettingsResponse>() {

        @Override
        public void onResponse(ClusterGetSettingsResponse response) {
        // <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.cluster().getSettingsAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-settings-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ClusterGetSettingsResponse(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) ClusterGetSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterGetSettingsRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 55 with LatchedActionListener

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

the class ClusterClientDocumentationIT method testRemoteInfoAsync.

public void testRemoteInfoAsync() throws Exception {
    setupRemoteClusterConfig("local_cluster");
    RestHighLevelClient client = highLevelClient();
    // tag::remote-info-request
    RemoteInfoRequest request = new RemoteInfoRequest();
    // end::remote-info-request
    // tag::remote-info-execute-listener
    ActionListener<RemoteInfoResponse> listener = new ActionListener<RemoteInfoResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::remote-info-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::health-execute-async
    // <1>
    client.cluster().remoteInfoAsync(request, RequestOptions.DEFAULT, listener);
    // end::health-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RemoteInfoResponse(org.opensearch.client.cluster.RemoteInfoResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) RemoteInfoRequest(org.opensearch.client.cluster.RemoteInfoRequest) IOException(java.io.IOException)

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