use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetMappingAsync.
public void testGetMappingAsync() throws Exception {
final RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
PutMappingRequest request = new PutMappingRequest("twitter");
request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON);
AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
assertTrue(putMappingResponse.isAcknowledged());
}
{
GetMappingsRequest request = new GetMappingsRequest();
request.indices("twitter");
// tag::get-mappings-execute-listener
ActionListener<GetMappingsResponse> listener = new ActionListener<GetMappingsResponse>() {
@Override
public void onResponse(GetMappingsResponse putMappingResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-mappings-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
final ActionListener<GetMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
listener = ActionListener.wrap(r -> {
Map<String, MappingMetadata> allMappings = r.mappings();
MappingMetadata indexMapping = allMappings.get("twitter");
Map<String, Object> mapping = indexMapping.sourceAsMap();
Map<String, String> type = new HashMap<>();
type.put("type", "text");
Map<String, Object> field = new HashMap<>();
field.put("message", type);
Map<String, Object> expected = new HashMap<>();
expected.put("properties", field);
assertThat(mapping, equalTo(expected));
latchListener.onResponse(r);
}, e -> {
latchListener.onFailure(e);
fail("should not fail");
});
// tag::get-mappings-execute-async
// <1>
client.indices().getMappingAsync(request, RequestOptions.DEFAULT, listener);
// end::get-mappings-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method indexSearchTestData.
private void indexSearchTestData() throws IOException {
CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("user").field("type", "keyword").field("doc_values", "false").endObject().endObject().endObject());
CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT);
assertTrue(authorsResponse.isAcknowledged());
CreateIndexRequest reviewersRequest = new CreateIndexRequest("contributors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("user").field("type", "keyword").field("store", "true").endObject().endObject().endObject());
CreateIndexResponse reviewersResponse = highLevelClient().indices().create(reviewersRequest, RequestOptions.DEFAULT);
assertTrue(reviewersResponse.isAcknowledged());
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "id", 1, "title", "In which order are my OpenSearch queries executed?", "user", Arrays.asList("foobar", "quxx"), "innerObject", Collections.singletonMap("key", "value")));
bulkRequest.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "id", 2, "title", "Current status and upcoming changes in OpenSearch", "user", Arrays.asList("foobar", "grault"), "innerObject", Collections.singletonMap("key", "value")));
bulkRequest.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "id", 3, "title", "The Future of Federated Search in OpenSearch", "user", Arrays.asList("foobar", "quuz"), "innerObject", Collections.singletonMap("key", "value")));
bulkRequest.add(new IndexRequest("authors").id("1").source(XContentType.JSON, "id", 1, "user", "foobar"));
bulkRequest.add(new IndexRequest("contributors").id("1").source(XContentType.JSON, "id", 1, "user", "quuz"));
bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT);
assertSame(RestStatus.OK, bulkResponse.status());
assertFalse(bulkResponse.hasFailures());
}
use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testMultiTermVectors.
// Not entirely sure if _mtermvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testMultiTermVectors() 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);
client.index(new IndexRequest("index").id("2").source("user", "baz"), RequestOptions.DEFAULT);
Response refreshResponse = client().performRequest(new Request("POST", "/authors/_refresh"));
assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
{
// tag::multi-term-vectors-request
// <1>
MultiTermVectorsRequest request = new MultiTermVectorsRequest();
TermVectorsRequest tvrequest1 = new TermVectorsRequest("authors", "1");
tvrequest1.setFields("user");
// <2>
request.add(tvrequest1);
XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("user", "guest-user").endObject();
TermVectorsRequest tvrequest2 = new TermVectorsRequest("authors", docBuilder);
// <3>
request.add(tvrequest2);
// end::multi-term-vectors-request
}
// tag::multi-term-vectors-request-template
TermVectorsRequest tvrequestTemplate = // <1>
new TermVectorsRequest("authors", "fake_id");
tvrequestTemplate.setFields("user");
String[] ids = { "1", "2" };
MultiTermVectorsRequest request = // <2>
new MultiTermVectorsRequest(ids, tvrequestTemplate);
// end::multi-term-vectors-request-template
// tag::multi-term-vectors-execute
MultiTermVectorsResponse response = client.mtermvectors(request, RequestOptions.DEFAULT);
// end::multi-term-vectors-execute
// tag::multi-term-vectors-response
List<TermVectorsResponse> tvresponseList = // <1>
response.getTermVectorsResponses();
if (tvresponseList != null) {
for (TermVectorsResponse tvresponse : tvresponseList) {
}
}
// end::multi-term-vectors-response
ActionListener<MultiTermVectorsResponse> listener;
// tag::multi-term-vectors-execute-listener
listener = new ActionListener<MultiTermVectorsResponse>() {
@Override
public void onResponse(MultiTermVectorsResponse mtvResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-term-vectors-execute-listener
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-term-vectors-execute-async
client.mtermvectorsAsync(request, RequestOptions.DEFAULT, // <1>
listener);
// end::multi-term-vectors-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.
the class ClusterClientDocumentationIT method testClusterHealth.
@SuppressWarnings("unused")
public void testClusterHealth() throws IOException {
RestHighLevelClient client = highLevelClient();
client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
{
// tag::health-request
ClusterHealthRequest request = new ClusterHealthRequest();
// end::health-request
}
{
// tag::health-request-indices-ctr
ClusterHealthRequest request = new ClusterHealthRequest("index1", "index2");
// end::health-request-indices-ctr
}
{
// tag::health-request-indices-setter
ClusterHealthRequest request = new ClusterHealthRequest();
request.indices("index1", "index2");
// end::health-request-indices-setter
}
ClusterHealthRequest request = new ClusterHealthRequest();
// tag::health-request-timeout
// <1>
request.timeout(TimeValue.timeValueSeconds(50));
// <2>
request.timeout("50s");
// end::health-request-timeout
// tag::health-request-master-timeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueSeconds(20));
// <2>
request.masterNodeTimeout("20s");
// end::health-request-master-timeout
// tag::health-request-wait-status
// <1>
request.waitForStatus(ClusterHealthStatus.YELLOW);
// <2>
request.waitForYellowStatus();
// end::health-request-wait-status
// tag::health-request-wait-events
// <1>
request.waitForEvents(Priority.NORMAL);
// end::health-request-wait-events
// tag::health-request-level
// <1>
request.level(ClusterHealthRequest.Level.SHARDS);
// end::health-request-level
// tag::health-request-wait-relocation
// <1>
request.waitForNoRelocatingShards(true);
// end::health-request-wait-relocation
// tag::health-request-wait-initializing
// <1>
request.waitForNoInitializingShards(true);
// end::health-request-wait-initializing
// tag::health-request-wait-nodes
// <1>
request.waitForNodes("2");
// <2>
request.waitForNodes(">=2");
// <3>
request.waitForNodes("le(2)");
// end::health-request-wait-nodes
// tag::health-request-wait-active
// <1>
request.waitForActiveShards(ActiveShardCount.ALL);
// <2>
request.waitForActiveShards(1);
// end::health-request-wait-active
// tag::health-request-local
// <1>
request.local(true);
// end::health-request-local
// tag::health-execute
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
// end::health-execute
assertThat(response.isTimedOut(), equalTo(false));
assertThat(response.status(), equalTo(RestStatus.OK));
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
assertThat(response, notNullValue());
// tag::health-response-general
// <1>
String clusterName = response.getClusterName();
// <2>
ClusterHealthStatus status = response.getStatus();
// end::health-response-general
// tag::health-response-request-status
// <1>
boolean timedOut = response.isTimedOut();
// <2>
RestStatus restStatus = response.status();
// end::health-response-request-status
// tag::health-response-nodes
// <1>
int numberOfNodes = response.getNumberOfNodes();
// <2>
int numberOfDataNodes = response.getNumberOfDataNodes();
// end::health-response-nodes
{
// tag::health-response-shards
// <1>
int activeShards = response.getActiveShards();
// <2>
int activePrimaryShards = response.getActivePrimaryShards();
// <3>
int relocatingShards = response.getRelocatingShards();
// <4>
int initializingShards = response.getInitializingShards();
// <5>
int unassignedShards = response.getUnassignedShards();
// <6>
int delayedUnassignedShards = response.getDelayedUnassignedShards();
// <7>
double activeShardsPercent = response.getActiveShardsPercent();
// end::health-response-shards
}
// tag::health-response-task
// <1>
TimeValue taskMaxWaitingTime = response.getTaskMaxWaitingTime();
// <2>
int numberOfPendingTasks = response.getNumberOfPendingTasks();
// <3>
int numberOfInFlightFetch = response.getNumberOfInFlightFetch();
// end::health-response-task
// tag::health-response-indices
// <1>
Map<String, ClusterIndexHealth> indices = response.getIndices();
// end::health-response-indices
{
// tag::health-response-index
// <1>
ClusterIndexHealth index = indices.get("index");
ClusterHealthStatus indexStatus = index.getStatus();
int numberOfShards = index.getNumberOfShards();
int numberOfReplicas = index.getNumberOfReplicas();
int activeShards = index.getActiveShards();
int activePrimaryShards = index.getActivePrimaryShards();
int initializingShards = index.getInitializingShards();
int relocatingShards = index.getRelocatingShards();
int unassignedShards = index.getUnassignedShards();
// end::health-response-index
// tag::health-response-shard-details
// <1>
Map<Integer, ClusterShardHealth> shards = index.getShards();
ClusterShardHealth shardHealth = shards.get(0);
int shardId = shardHealth.getShardId();
ClusterHealthStatus shardStatus = shardHealth.getStatus();
int active = shardHealth.getActiveShards();
int initializing = shardHealth.getInitializingShards();
int unassigned = shardHealth.getUnassignedShards();
int relocating = shardHealth.getRelocatingShards();
boolean primaryActive = shardHealth.isPrimaryActive();
// end::health-response-shard-details
}
}
use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testRolloverIndex.
public void testRolloverIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
client.indices().create(new CreateIndexRequest("index-1").alias(new Alias("alias")), RequestOptions.DEFAULT);
}
// tag::rollover-index-request
// <1>
RolloverRequest request = new RolloverRequest("alias", "index-2");
// <2>
request.addMaxIndexAgeCondition(new TimeValue(7, TimeUnit.DAYS));
// <3>
request.addMaxIndexDocsCondition(1000);
// <4>
request.addMaxIndexSizeCondition(new ByteSizeValue(5, ByteSizeUnit.GB));
// end::rollover-index-request
// tag::rollover-index-request-timeout
// <1>
request.setTimeout(TimeValue.timeValueMinutes(2));
// end::rollover-index-request-timeout
// tag::rollover-index-request-masterTimeout
// <1>
request.setMasterTimeout(TimeValue.timeValueMinutes(1));
// end::rollover-index-request-masterTimeout
// tag::rollover-index-request-dryRun
// <1>
request.dryRun(true);
// end::rollover-index-request-dryRun
// tag::rollover-index-request-waitForActiveShards
// <1>
request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.from(2));
// <2>
request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.DEFAULT);
// end::rollover-index-request-waitForActiveShards
// tag::rollover-index-request-settings
request.getCreateIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
4));
// end::rollover-index-request-settings
// tag::rollover-index-request-mapping
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"keyword\"}}}";
// <1>
request.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
// end::rollover-index-request-mapping
// tag::rollover-index-request-alias
// <1>
request.getCreateIndexRequest().alias(new Alias("another_alias"));
// end::rollover-index-request-alias
// tag::rollover-index-execute
RolloverResponse rolloverResponse = client.indices().rollover(request, RequestOptions.DEFAULT);
// end::rollover-index-execute
// tag::rollover-index-response
// <1>
boolean acknowledged = rolloverResponse.isAcknowledged();
// <2>
boolean shardsAcked = rolloverResponse.isShardsAcknowledged();
// <3>
String oldIndex = rolloverResponse.getOldIndex();
// <4>
String newIndex = rolloverResponse.getNewIndex();
// <5>
boolean isRolledOver = rolloverResponse.isRolledOver();
// <6>
boolean isDryRun = rolloverResponse.isDryRun();
// <7>
Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
// end::rollover-index-response
assertFalse(acknowledged);
assertFalse(shardsAcked);
assertEquals("index-1", oldIndex);
assertEquals("index-2", newIndex);
assertFalse(isRolledOver);
assertTrue(isDryRun);
assertEquals(3, conditionStatus.size());
// tag::rollover-index-execute-listener
ActionListener<RolloverResponse> listener = new ActionListener<RolloverResponse>() {
@Override
public void onResponse(RolloverResponse rolloverResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::rollover-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::rollover-index-execute-async
// <1>
client.indices().rolloverAsync(request, RequestOptions.DEFAULT, listener);
// end::rollover-index-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations