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));
}
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
}
}
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));
}
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));
}
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));
}
Aggregations