use of org.opensearch.client.indices.GetFieldMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConverters method getFieldMapping.
static Request getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) {
String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices();
String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields();
String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs("_mapping").addPathPartAsIs("field").addCommaSeparatedPathParts(fields).build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withIndicesOptions(getFieldMappingsRequest.indicesOptions());
parameters.withIncludeDefaults(getFieldMappingsRequest.includeDefaults());
request.addParameters(parameters.asMap());
return request;
}
use of org.opensearch.client.indices.GetFieldMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testGetFieldMapping.
public void testGetFieldMapping() 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());
GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest().indices(indexName).fields("field");
GetFieldMappingsResponse getFieldMappingsResponse = execute(getFieldMappingsRequest, highLevelClient().indices()::getFieldMapping, highLevelClient().indices()::getFieldMappingAsync);
final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappingMap = getFieldMappingsResponse.mappings().get(indexName);
final GetFieldMappingsResponse.FieldMappingMetadata metadata = new GetFieldMappingsResponse.FieldMappingMetadata("field", new BytesArray("{\"field\":{\"type\":\"text\"}}"));
assertThat(fieldMappingMap, equalTo(Collections.singletonMap("field", metadata)));
}
use of org.opensearch.client.indices.GetFieldMappingsRequest 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.GetFieldMappingsRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConvertersTests method testGetFieldMapping.
public void testGetFieldMapping() {
GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5);
getFieldMappingsRequest.indices(indices);
} else if (randomBoolean()) {
getFieldMappingsRequest.indices((String[]) null);
}
String[] fields = null;
if (randomBoolean()) {
fields = new String[randomIntBetween(1, 5)];
for (int i = 0; i < fields.length; i++) {
fields[i] = randomAlphaOfLengthBetween(3, 10);
}
getFieldMappingsRequest.fields(fields);
} else if (randomBoolean()) {
getFieldMappingsRequest.fields((String[]) null);
}
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomIndicesOptions(getFieldMappingsRequest::indicesOptions, getFieldMappingsRequest::indicesOptions, expectedParams);
Request request = IndicesRequestConverters.getFieldMapping(getFieldMappingsRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
endpoint.add("field");
if (fields != null) {
endpoint.add(String.join(",", fields));
}
Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
Assert.assertThat(expectedParams, equalTo(request.getParameters()));
Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}
Aggregations