use of org.opensearch.client.indices.CreateIndexResponse in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method newIndex.
@Override
public String newIndex() throws CalFacadeException {
try {
final String newName = "bw" + docType.toLowerCase() + newIndexSuffix();
targetIndex = newName;
final CreateIndexRequest req = new CreateIndexRequest(newName);
final String mappingStr = fileToString(Util.buildPath(false, idxpars.getIndexerConfig(), "/", docType.toLowerCase(), "/mappings.json"));
req.source(mappingStr, XContentType.JSON);
info("Attempt to create index " + newName);
final CreateIndexResponse resp = getClient().indices().create(req, RequestOptions.DEFAULT);
info("Index " + newName + " created");
final DocBuilder db = getDocBuilder();
indexDoc(db.makeUpdateInfoDoc(docType), true);
return newName;
} catch (final CalFacadeException cfe) {
throw cfe;
} catch (final Throwable t) {
error(t);
throw new CalFacadeException(t);
}
}
use of org.opensearch.client.indices.CreateIndexResponse 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.CreateIndexResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testIndexPutSettings.
@SuppressWarnings("unused")
public void testIndexPutSettings() throws Exception {
RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
// tag::indices-put-settings-request
// <1>
UpdateSettingsRequest request = new UpdateSettingsRequest("index1");
UpdateSettingsRequest requestMultiple = // <2>
new UpdateSettingsRequest("index1", "index2");
// <3>
UpdateSettingsRequest requestAll = new UpdateSettingsRequest();
// end::indices-put-settings-request
// tag::indices-put-settings-create-settings
String settingKey = "index.number_of_replicas";
int settingValue = 0;
Settings settings = Settings.builder().put(settingKey, settingValue).build();
// end::indices-put-settings-create-settings
// tag::indices-put-settings-request-index-settings
request.settings(settings);
// end::indices-put-settings-request-index-settings
{
// tag::indices-put-settings-settings-builder
Settings.Builder settingsBuilder = Settings.builder().put(settingKey, settingValue);
// <1>
request.settings(settingsBuilder);
// end::indices-put-settings-settings-builder
}
{
// tag::indices-put-settings-settings-map
Map<String, Object> map = new HashMap<>();
map.put(settingKey, settingValue);
// <1>
request.settings(map);
// end::indices-put-settings-settings-map
}
{
// tag::indices-put-settings-settings-source
request.settings("{\"index.number_of_replicas\": \"2\"}", // <1>
XContentType.JSON);
// end::indices-put-settings-settings-source
}
// tag::indices-put-settings-request-preserveExisting
// <1>
request.setPreserveExisting(false);
// end::indices-put-settings-request-preserveExisting
// tag::indices-put-settings-request-timeout
// <1>
request.timeout(TimeValue.timeValueMinutes(2));
// <2>
request.timeout("2m");
// end::indices-put-settings-request-timeout
// tag::indices-put-settings-request-masterTimeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
// <2>
request.masterNodeTimeout("1m");
// end::indices-put-settings-request-masterTimeout
// tag::indices-put-settings-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::indices-put-settings-request-indicesOptions
// tag::indices-put-settings-execute
AcknowledgedResponse updateSettingsResponse = client.indices().putSettings(request, RequestOptions.DEFAULT);
// end::indices-put-settings-execute
// tag::indices-put-settings-response
// <1>
boolean acknowledged = updateSettingsResponse.isAcknowledged();
// end::indices-put-settings-response
assertTrue(acknowledged);
// tag::indices-put-settings-execute-listener
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse updateSettingsResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::indices-put-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::indices-put-settings-execute-async
// <1>
client.indices().putSettingsAsync(request, RequestOptions.DEFAULT, listener);
// end::indices-put-settings-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.indices.CreateIndexResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetFieldMapping.
@SuppressWarnings("unused")
public void testGetFieldMapping() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
PutMappingRequest request = new PutMappingRequest("twitter");
request.source("{\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"timestamp\": {\n" + " \"type\": \"date\"\n" + " }\n" + " }\n" + // <1>
"}", XContentType.JSON);
AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
assertTrue(putMappingResponse.isAcknowledged());
}
// tag::get-field-mappings-request
// <1>
GetFieldMappingsRequest request = new GetFieldMappingsRequest();
// <2>
request.indices("twitter");
// <3>
request.fields("message", "timestamp");
// end::get-field-mappings-request
// tag::get-field-mappings-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::get-field-mappings-request-indicesOptions
// tag::get-field-mappings-request-local
// <1>
request.local(true);
// end::get-field-mappings-request-local
{
// tag::get-field-mappings-execute
GetFieldMappingsResponse response = client.indices().getFieldMapping(request, RequestOptions.DEFAULT);
// end::get-field-mappings-execute
// tag::get-field-mappings-response
final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = // <1>
response.mappings();
final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = // <2>
mappings.get("twitter");
final GetFieldMappingsResponse.FieldMappingMetadata metadata = // <3>
fieldMappings.get("message");
// <4>
final String fullName = metadata.fullName();
// <5>
final Map<String, Object> source = metadata.sourceAsMap();
// end::get-field-mappings-response
}
{
// tag::get-field-mappings-execute-listener
ActionListener<GetFieldMappingsResponse> listener = new ActionListener<GetFieldMappingsResponse>() {
@Override
public void onResponse(GetFieldMappingsResponse putMappingResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-field-mappings-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
final ActionListener<GetFieldMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
listener = ActionListener.wrap(r -> {
final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = r.mappings();
final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = mappings.get("twitter");
final GetFieldMappingsResponse.FieldMappingMetadata metadata1 = fieldMappings.get("message");
final String fullName = metadata1.fullName();
final Map<String, Object> source = metadata1.sourceAsMap();
latchListener.onResponse(r);
}, e -> {
latchListener.onFailure(e);
fail("should not fail");
});
// tag::get-field-mappings-execute-async
// <1>
client.indices().getFieldMappingAsync(request, RequestOptions.DEFAULT, listener);
// end::get-field-mappings-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
use of org.opensearch.client.indices.CreateIndexResponse 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));
}
}
Aggregations