Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get mappings support to high-level rest client #30889

Merged
merged 14 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.client;

import org.apache.http.Header;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
Expand All @@ -38,6 +39,8 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand Down Expand Up @@ -134,11 +137,34 @@ public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header
* Put Mapping API on elastic.co</a>
*/
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
Header... headers) {
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, RequestConverters::putMapping,
PutMappingResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Retrieves the mappings on an index or indices using the Get Mapping API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
* Get Mapping API on elastic.co</a>
*/
public GetMappingsResponse getMappings(GetMappingsRequest getMappingsRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, RequestConverters::getMappings,
GetMappingsResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously retrieves the mappings on an index on indices using the Get Mapping API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
* Get Mapping API on elastic.co</a>
*/
public void getMappingsAsync(GetMappingsRequest getMappingsRequest, ActionListener<GetMappingsResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, RequestConverters::getMappings,
GetMappingsResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Updates aliases using the Index Aliases API
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
Expand Down Expand Up @@ -195,6 +196,19 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
return request;
}

static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException {
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();

Request request = new Request(HttpGet.METHOD_NAME, endpoint(indices, "_mapping", types));

Params parameters = new Params(request);
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
parameters.withLocal(getMappingsRequest.local());
return request;
}

static Request refresh(RefreshRequest refreshRequest) {
String[] indices = refreshRequest.indices() == null ? Strings.EMPTY_ARRAY : refreshRequest.indices();
Request request = new Request(HttpPost.METHOD_NAME, endpoint(indices, "_refresh"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand Down Expand Up @@ -79,6 +81,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
Expand Down Expand Up @@ -328,6 +331,42 @@ public void testPutMapping() throws IOException {
}
}

public void testGetMapping() throws IOException {
String indexName = "test";
createIndex(indexName, Settings.EMPTY);

PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
putMappingRequest.type("_doc");
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
mappingBuilder.startObject().startObject("properties").startObject("field");
mappingBuilder.field("type", "text");
mappingBuilder.endObject().endObject().endObject();
putMappingRequest.source(mappingBuilder);

PutMappingResponse putMappingResponse =
execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
assertTrue(putMappingResponse.isAcknowledged());

Map<String, Object> getIndexResponse = getAsMap(indexName);
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings._doc.properties.field.type", getIndexResponse));

GetMappingsRequest request = new GetMappingsRequest()
.indices(indexName)
.types("_doc");

GetMappingsResponse getMappingsResponse =
execute(request, highLevelClient().indices()::getMappings, highLevelClient().indices()::getMappingsAsync);

Map<String, Object> mappings = getMappingsResponse.getMappings().get(indexName).get("_doc").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));
}

public void testDeleteIndex() throws IOException {
{
// Delete index if exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
Expand Down Expand Up @@ -403,6 +404,47 @@ public void testPutMapping() throws IOException {
assertToXContentBody(putMappingRequest, request.getEntity());
}

public void testGetMapping() throws IOException {
GetMappingsRequest getMappingRequest = new GetMappingsRequest();

String[] indices = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
indices = randomIndicesNames(0, 5);
getMappingRequest.indices(indices);
} else if (randomBoolean()) {
getMappingRequest.indices((String[]) null);
}

String type = null;
if (randomBoolean()) {
type = randomAlphaOfLengthBetween(3, 10);
getMappingRequest.types(type);
} else if (randomBoolean()) {
getMappingRequest.types((String[]) null);
}

Map<String, String> expectedParams = new HashMap<>();

setRandomIndicesOptions(getMappingRequest::indicesOptions, getMappingRequest::indicesOptions, expectedParams);
setRandomMasterTimeout(getMappingRequest, expectedParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test around the local flag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, was added

setRandomLocal(getMappingRequest, expectedParams);

Request request = RequestConverters.getMappings(getMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
if (type != null) {
endpoint.add(type);
}
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));

assertThat(expectedParams, equalTo(request.getParameters()));
assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}

public void testDeleteIndex() {
String[] indices = randomIndicesNames(0, 5);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
Expand All @@ -64,6 +66,8 @@
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.SyncedFlushResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand All @@ -81,6 +85,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.equalTo;

/**
* This class is used to generate the Java Indices API documentation.
* You need to wrap your code between two tags like:
Expand Down Expand Up @@ -532,17 +538,17 @@ public void testPutMappingAsync() throws Exception {

// tag::put-mapping-execute-listener
ActionListener<PutMappingResponse> listener =
new ActionListener<PutMappingResponse>() {
@Override
public void onResponse(PutMappingResponse putMappingResponse) {
// <1>
}
new ActionListener<PutMappingResponse>() {
@Override
public void onResponse(PutMappingResponse putMappingResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::put-mapping-execute-listener

// Replace the empty listener by a blocking listener in test
Expand All @@ -557,6 +563,133 @@ public void onFailure(Exception e) {
}
}

public void testGetMapping() throws IOException {
RestHighLevelClient client = highLevelClient();

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
PutMappingRequest request = new PutMappingRequest("twitter");
request.type("tweet");
request.source(
"{\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", // <1>
XContentType.JSON);
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
assertTrue(putMappingResponse.isAcknowledged());
}

{
// tag::get-mapping-request
GetMappingsRequest request = new GetMappingsRequest(); // <1>
request.indices("twitter"); // <2>
request.types("tweet"); // <3>
// end::get-mapping-request

// tag::get-mapping-request-masterTimeout
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
request.masterNodeTimeout("1m"); // <2>
// end::get-mapping-request-masterTimeout

// tag::get-mapping-request-indicesOptions
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
// end::get-mapping-request-indicesOptions

// tag::get-mapping-execute
GetMappingsResponse getMappingResponse = client.indices().getMappings(request);
// end::get-mapping-execute

// tag::get-mapping-response
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = getMappingResponse.mappings(); // <1>
MappingMetaData typeMapping = allMappings.get("twitter").get("tweet"); // <2>
Map<String, Object> tweetMapping = typeMapping.sourceAsMap(); // <3>
// end::get-mapping-response

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(tweetMapping, equalTo(expected));
}
}

public void testGetMappingAsync() throws Exception {
final RestHighLevelClient client = highLevelClient();

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
PutMappingRequest request = new PutMappingRequest("twitter");
request.type("tweet");
request.source(
"{\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", // <1>
XContentType.JSON);
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
assertTrue(putMappingResponse.isAcknowledged());
}

{
GetMappingsRequest request = new GetMappingsRequest();
request.indices("twitter");
request.types("tweet");

// tag::get-mapping-execute-listener
ActionListener<GetMappingsResponse> listener =
new ActionListener<GetMappingsResponse>() {
@Override
public void onResponse(GetMappingsResponse putMappingResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-mapping-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 -> {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = r.mappings();
MappingMetaData typeMapping = allMappings.get("twitter").get("tweet");
Map<String, Object> tweetMapping = typeMapping.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(tweetMapping, equalTo(expected));
latchListener.onResponse(r);
}, e -> {
latchListener.onFailure(e);
fail("should not fail");
});

// tag::get-mapping-execute-async
client.indices().getMappingsAsync(request, listener); // <1>
// end::get-mapping-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}

public void testOpenIndex() throws Exception {
RestHighLevelClient client = highLevelClient();

Expand Down
Loading