use of org.opensearch.client.indices.GetMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConvertersTests method testGetMapping.
public void testGetMapping() {
GetMappingsRequest getMappingRequest = new GetMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5);
getMappingRequest.indices(indices);
} else if (randomBoolean()) {
getMappingRequest.indices((String[]) null);
}
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomIndicesOptions(getMappingRequest::indicesOptions, getMappingRequest::indicesOptions, expectedParams);
RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams);
RequestConvertersTests.setRandomLocal(getMappingRequest::local, expectedParams);
Request request = IndicesRequestConverters.getMappings(getMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
Assert.assertThat(expectedParams, equalTo(request.getParameters()));
Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}
use of org.opensearch.client.indices.GetMappingsRequest 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.GetMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConverters method getMappings.
static Request getMappings(GetMappingsRequest getMappingsRequest) {
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
Request request = new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping"));
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
parameters.withLocal(getMappingsRequest.local());
request.addParameters(parameters.asMap());
return request;
}
use of org.opensearch.client.indices.GetMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testGetMapping.
public void testGetMapping() throws IOException {
String indexName = "test";
createIndex(indexName, Settings.EMPTY);
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
mappingBuilder.startObject().startObject("properties").startObject("field");
mappingBuilder.field("type", "text");
mappingBuilder.endObject().endObject().endObject();
putMappingRequest.source(mappingBuilder);
AcknowledgedResponse putMappingResponse = execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
assertTrue(putMappingResponse.isAcknowledged());
Map<String, Object> getIndexResponse = getAsMap(indexName);
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
GetMappingsRequest request = new GetMappingsRequest().indices(indexName);
GetMappingsResponse getMappingsResponse = execute(request, highLevelClient().indices()::getMapping, highLevelClient().indices()::getMappingAsync);
Map<String, Object> mappings = getMappingsResponse.mappings().get(indexName).sourceAsMap();
Map<String, String> type = new HashMap<>();
type.put("type", "text");
Map<String, Object> field = new HashMap<>();
field.put("field", type);
Map<String, Object> expected = new HashMap<>();
expected.put("properties", field);
assertThat(mappings, equalTo(expected));
}
Aggregations