Skip to content

Commit

Permalink
Merge remote-tracking branch 'es/6.x' into ccr-6.x
Browse files Browse the repository at this point in the history
* es/6.x:
  Remove redundant argument for buildConfiguration of s3 plugin (#28281)
  Completely remove Painless Type from AnalyzerCaster in favor of Java Class. (#28329)
  Fix spelling error
  Reindex: Wait for deletion in test
  Reindex: log more on rare test failure
  Ensure we protect Collections obtained from scripts from self-referencing (#28335)
  Provide a better error message for the case when all shards failed (#28333)
  REST high-level client: add support for update_all_types option to put mapping API
  Added Put Mapping API to high-level Rest client (#27869)
  [Test] Re-Add integer_range and date_range field types for query builder tests (#28171)
  Revert change that does not return all indices if a specific alias is requested via get alias api. (#28294)
  Painless: Replace Painless Type with Java Class during Casts (#27847)
  Notify affixMap settings when any under the registered prefix matches (#28317)
  • Loading branch information
martijnvg committed Jan 24, 2018
2 parents 4974603 + 58008e1 commit 1351efd
Show file tree
Hide file tree
Showing 59 changed files with 1,665 additions and 802 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;

Expand Down Expand Up @@ -89,6 +91,29 @@ public void createAsync(CreateIndexRequest createIndexRequest, ActionListener<Cr
listener, Collections.emptySet(), headers);
}

/**
* Updates the mappings on an index using the Put Mapping API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html">
* Put Mapping API on elastic.co</a>
*/
public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
Collections.emptySet(), headers);
}

/**
* Asynchronously updates the mappings on an index using the Put Mapping API
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html">
* Put Mapping API on elastic.co</a>
*/
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
listener, Collections.emptySet(), headers);
}

/**
* Opens an index using the Open Index API
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
Expand Down Expand Up @@ -179,6 +180,23 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
}

static Request putMapping(PutMappingRequest putMappingRequest) throws IOException {
// The concreteIndex is an internal concept, not applicable to requests made over the REST API.
if (putMappingRequest.getConcreteIndex() != null) {
throw new IllegalArgumentException("concreteIndex cannot be set on PutMapping requests made over the REST API");
}

String endpoint = endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type());

Params parameters = Params.builder();
parameters.withTimeout(putMappingRequest.timeout());
parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout());
parameters.withUpdateAllTypes(putMappingRequest.updateAllTypes());

HttpEntity entity = createEntity(putMappingRequest, REQUEST_BODY_CONTENT_TYPE);
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
}

static Request info() {
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
}
Expand Down Expand Up @@ -455,6 +473,10 @@ static String endpoint(String[] indices, String[] types, String endpoint) {
return endpoint(String.join(",", indices), String.join(",", types), endpoint);
}

static String endpoint(String[] indices, String endpoint, String type) {
return endpoint(String.join(",", indices), endpoint, type);
}

/**
* Utility method to build request's endpoint.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.support.ActiveShardCount;
Expand Down Expand Up @@ -109,6 +111,35 @@ public void testCreateIndex() throws IOException {
}
}

@SuppressWarnings("unchecked")
public void testPutMapping() throws IOException {
{
// Add mappings to index
String indexName = "mapping_index";
createIndex(indexName);

PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
putMappingRequest.type("type_name");
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> indexMetaData = getIndexMetadata(indexName);
Map<String, Object> mappingsData = (Map) indexMetaData.get("mappings");
Map<String, Object> typeData = (Map) mappingsData.get("type_name");
Map<String, Object> properties = (Map) typeData.get("properties");
Map<String, Object> field = (Map) properties.get("field");

assertEquals("text", field.get("type"));
}
}

public void testDeleteIndex() throws IOException {
{
// Delete index if exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkShardRequest;
Expand Down Expand Up @@ -309,14 +310,7 @@ public void testCreateIndex() throws IOException {
setRandomTimeout(createIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
setRandomMasterTimeout(createIndexRequest, expectedParams);
setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, ActiveShardCount.DEFAULT, expectedParams);

if (randomBoolean()) {
boolean updateAllTypes = randomBoolean();
createIndexRequest.updateAllTypes(updateAllTypes);
if (updateAllTypes) {
expectedParams.put("update_all_types", Boolean.TRUE.toString());
}
}
setRandomUpdateAllTypes(createIndexRequest::updateAllTypes, expectedParams);

Request request = Request.createIndex(createIndexRequest);
assertEquals("/" + indexName, request.getEndpoint());
Expand All @@ -325,6 +319,40 @@ public void testCreateIndex() throws IOException {
assertToXContentBody(createIndexRequest, request.getEntity());
}

public void testPutMapping() throws IOException {
PutMappingRequest putMappingRequest = new PutMappingRequest();

int numIndices = randomIntBetween(0, 5);
String[] indices = new String[numIndices];
for (int i = 0; i < numIndices; i++) {
indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5);
}
putMappingRequest.indices(indices);

String type = randomAlphaOfLengthBetween(3, 10);
putMappingRequest.type(type);

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

setRandomTimeout(putMappingRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
setRandomMasterTimeout(putMappingRequest, expectedParams);
setRandomUpdateAllTypes(putMappingRequest::updateAllTypes, expectedParams);

Request request = Request.putMapping(putMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
endpoint.add(type);
assertEquals(endpoint.toString(), request.getEndpoint());

assertEquals(expectedParams, request.getParameters());
assertEquals("PUT", request.getMethod());
assertToXContentBody(putMappingRequest, request.getEntity());
}

public void testDeleteIndex() {
String[] indices = randomIndicesNames(0, 5);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices);
Expand Down Expand Up @@ -1094,6 +1122,16 @@ private static void setRandomWaitForActiveShards(Consumer<ActiveShardCount> sett
}
}

private static void setRandomUpdateAllTypes(Consumer<Boolean> setter, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean updateAllTypes = randomBoolean();
setter.accept(updateAllTypes);
if (updateAllTypes) {
expectedParams.put("update_all_types", Boolean.TRUE.toString());
}
}
}

private static void setRandomRefreshPolicy(Consumer<WriteRequest.RefreshPolicy> setter, Map<String, String> expectedParams) {
if (randomBoolean()) {
WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.support.ActiveShardCount;
Expand Down Expand Up @@ -157,15 +159,15 @@ public void testCreateIndex() throws IOException {

// tag::create-index-request-mappings
request.mapping("tweet", // <1>
" {\n" +
" \"tweet\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
"{\n" +
" \"tweet\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }", // <2>
" }\n" +
"}", // <2>
XContentType.JSON);
// end::create-index-request-mappings

Expand Down Expand Up @@ -228,6 +230,86 @@ public void onFailure(Exception e) {
}
}

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

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
}

{
// tag::put-mapping-request
PutMappingRequest request = new PutMappingRequest("twitter"); // <1>
request.type("tweet"); // <2>
// end::put-mapping-request

// tag::put-mapping-request-source
request.source(
"{\n" +
" \"tweet\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}", // <1>
XContentType.JSON);
// end::put-mapping-request-source

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

// tag::put-mapping-execute
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
// end::put-mapping-execute

// tag::put-mapping-response
boolean acknowledged = putMappingResponse.isAcknowledged(); // <1>
// end::put-mapping-response
assertTrue(acknowledged);
}
}

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

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
assertTrue(createIndexResponse.isAcknowledged());
}

{
PutMappingRequest request = new PutMappingRequest("twitter").type("tweet");
// tag::put-mapping-execute-async
client.indices().putMappingAsync(request, new ActionListener<PutMappingResponse>() {
@Override
public void onResponse(PutMappingResponse putMappingResponse) {
// <1>
}

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

assertBusy(() -> {
// TODO Use Indices Exist API instead once it exists
Response response = client.getLowLevelClient().performRequest("HEAD", "twitter");
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
});
}
}

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

Expand Down
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/apis/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include::open_index.asciidoc[]

include::close_index.asciidoc[]

include::putmapping.asciidoc[]

include::_index.asciidoc[]

include::get.asciidoc[]
Expand Down
Loading

0 comments on commit 1351efd

Please sign in to comment.