Skip to content

Commit 1351efd

Browse files
committed
Merge remote-tracking branch 'es/6.x' into ccr-6.x
* 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)
2 parents 4974603 + 58008e1 commit 1351efd

File tree

59 files changed

+1665
-802
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1665
-802
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
2828
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
2929
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
30+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
31+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
3032
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3133
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
3234

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

94+
/**
95+
* Updates the mappings on an index using the Put Mapping API
96+
* <p>
97+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html">
98+
* Put Mapping API on elastic.co</a>
99+
*/
100+
public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header... headers) throws IOException {
101+
return restHighLevelClient.performRequestAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
102+
Collections.emptySet(), headers);
103+
}
104+
105+
/**
106+
* Asynchronously updates the mappings on an index using the Put Mapping API
107+
* <p>
108+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html">
109+
* Put Mapping API on elastic.co</a>
110+
*/
111+
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
112+
Header... headers) {
113+
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, Request::putMapping, PutMappingResponse::fromXContent,
114+
listener, Collections.emptySet(), headers);
115+
}
116+
92117
/**
93118
* Opens an index using the Open Index API
94119
* <p>

client/rest-high-level/src/main/java/org/elasticsearch/client/Request.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
3333
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3434
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
35+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
3536
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3637
import org.elasticsearch.action.bulk.BulkRequest;
3738
import org.elasticsearch.action.delete.DeleteRequest;
@@ -179,6 +180,23 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep
179180
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
180181
}
181182

183+
static Request putMapping(PutMappingRequest putMappingRequest) throws IOException {
184+
// The concreteIndex is an internal concept, not applicable to requests made over the REST API.
185+
if (putMappingRequest.getConcreteIndex() != null) {
186+
throw new IllegalArgumentException("concreteIndex cannot be set on PutMapping requests made over the REST API");
187+
}
188+
189+
String endpoint = endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type());
190+
191+
Params parameters = Params.builder();
192+
parameters.withTimeout(putMappingRequest.timeout());
193+
parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout());
194+
parameters.withUpdateAllTypes(putMappingRequest.updateAllTypes());
195+
196+
HttpEntity entity = createEntity(putMappingRequest, REQUEST_BODY_CONTENT_TYPE);
197+
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity);
198+
}
199+
182200
static Request info() {
183201
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null);
184202
}
@@ -455,6 +473,10 @@ static String endpoint(String[] indices, String[] types, String endpoint) {
455473
return endpoint(String.join(",", indices), String.join(",", types), endpoint);
456474
}
457475

476+
static String endpoint(String[] indices, String endpoint, String type) {
477+
return endpoint(String.join(",", indices), endpoint, type);
478+
}
479+
458480
/**
459481
* Utility method to build request's endpoint.
460482
*/

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
2828
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
2929
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
30+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
31+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
3032
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3133
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
3234
import org.elasticsearch.action.support.ActiveShardCount;
@@ -109,6 +111,35 @@ public void testCreateIndex() throws IOException {
109111
}
110112
}
111113

114+
@SuppressWarnings("unchecked")
115+
public void testPutMapping() throws IOException {
116+
{
117+
// Add mappings to index
118+
String indexName = "mapping_index";
119+
createIndex(indexName);
120+
121+
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
122+
putMappingRequest.type("type_name");
123+
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
124+
mappingBuilder.startObject().startObject("properties").startObject("field");
125+
mappingBuilder.field("type", "text");
126+
mappingBuilder.endObject().endObject().endObject();
127+
putMappingRequest.source(mappingBuilder);
128+
129+
PutMappingResponse putMappingResponse =
130+
execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
131+
assertTrue(putMappingResponse.isAcknowledged());
132+
133+
Map<String, Object> indexMetaData = getIndexMetadata(indexName);
134+
Map<String, Object> mappingsData = (Map) indexMetaData.get("mappings");
135+
Map<String, Object> typeData = (Map) mappingsData.get("type_name");
136+
Map<String, Object> properties = (Map) typeData.get("properties");
137+
Map<String, Object> field = (Map) properties.get("field");
138+
139+
assertEquals("text", field.get("type"));
140+
}
141+
}
142+
112143
public void testDeleteIndex() throws IOException {
113144
{
114145
// Delete index if exists

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
2929
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
3030
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
31+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
3132
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3233
import org.elasticsearch.action.bulk.BulkRequest;
3334
import org.elasticsearch.action.bulk.BulkShardRequest;
@@ -309,14 +310,7 @@ public void testCreateIndex() throws IOException {
309310
setRandomTimeout(createIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
310311
setRandomMasterTimeout(createIndexRequest, expectedParams);
311312
setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, ActiveShardCount.DEFAULT, expectedParams);
312-
313-
if (randomBoolean()) {
314-
boolean updateAllTypes = randomBoolean();
315-
createIndexRequest.updateAllTypes(updateAllTypes);
316-
if (updateAllTypes) {
317-
expectedParams.put("update_all_types", Boolean.TRUE.toString());
318-
}
319-
}
313+
setRandomUpdateAllTypes(createIndexRequest::updateAllTypes, expectedParams);
320314

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

322+
public void testPutMapping() throws IOException {
323+
PutMappingRequest putMappingRequest = new PutMappingRequest();
324+
325+
int numIndices = randomIntBetween(0, 5);
326+
String[] indices = new String[numIndices];
327+
for (int i = 0; i < numIndices; i++) {
328+
indices[i] = "index-" + randomAlphaOfLengthBetween(2, 5);
329+
}
330+
putMappingRequest.indices(indices);
331+
332+
String type = randomAlphaOfLengthBetween(3, 10);
333+
putMappingRequest.type(type);
334+
335+
Map<String, String> expectedParams = new HashMap<>();
336+
337+
setRandomTimeout(putMappingRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
338+
setRandomMasterTimeout(putMappingRequest, expectedParams);
339+
setRandomUpdateAllTypes(putMappingRequest::updateAllTypes, expectedParams);
340+
341+
Request request = Request.putMapping(putMappingRequest);
342+
StringJoiner endpoint = new StringJoiner("/", "/", "");
343+
String index = String.join(",", indices);
344+
if (Strings.hasLength(index)) {
345+
endpoint.add(index);
346+
}
347+
endpoint.add("_mapping");
348+
endpoint.add(type);
349+
assertEquals(endpoint.toString(), request.getEndpoint());
350+
351+
assertEquals(expectedParams, request.getParameters());
352+
assertEquals("PUT", request.getMethod());
353+
assertToXContentBody(putMappingRequest, request.getEntity());
354+
}
355+
328356
public void testDeleteIndex() {
329357
String[] indices = randomIndicesNames(0, 5);
330358
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices);
@@ -1094,6 +1122,16 @@ private static void setRandomWaitForActiveShards(Consumer<ActiveShardCount> sett
10941122
}
10951123
}
10961124

1125+
private static void setRandomUpdateAllTypes(Consumer<Boolean> setter, Map<String, String> expectedParams) {
1126+
if (randomBoolean()) {
1127+
boolean updateAllTypes = randomBoolean();
1128+
setter.accept(updateAllTypes);
1129+
if (updateAllTypes) {
1130+
expectedParams.put("update_all_types", Boolean.TRUE.toString());
1131+
}
1132+
}
1133+
}
1134+
10971135
private static void setRandomRefreshPolicy(Consumer<WriteRequest.RefreshPolicy> setter, Map<String, String> expectedParams) {
10981136
if (randomBoolean()) {
10991137
WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
2929
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3030
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
31+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
32+
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
3133
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3234
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
3335
import org.elasticsearch.action.support.ActiveShardCount;
@@ -157,15 +159,15 @@ public void testCreateIndex() throws IOException {
157159

158160
// tag::create-index-request-mappings
159161
request.mapping("tweet", // <1>
160-
" {\n" +
161-
" \"tweet\": {\n" +
162-
" \"properties\": {\n" +
163-
" \"message\": {\n" +
164-
" \"type\": \"text\"\n" +
165-
" }\n" +
162+
"{\n" +
163+
" \"tweet\": {\n" +
164+
" \"properties\": {\n" +
165+
" \"message\": {\n" +
166+
" \"type\": \"text\"\n" +
166167
" }\n" +
167168
" }\n" +
168-
" }", // <2>
169+
" }\n" +
170+
"}", // <2>
169171
XContentType.JSON);
170172
// end::create-index-request-mappings
171173

@@ -228,6 +230,86 @@ public void onFailure(Exception e) {
228230
}
229231
}
230232

233+
public void testPutMapping() throws IOException {
234+
RestHighLevelClient client = highLevelClient();
235+
236+
{
237+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
238+
assertTrue(createIndexResponse.isAcknowledged());
239+
}
240+
241+
{
242+
// tag::put-mapping-request
243+
PutMappingRequest request = new PutMappingRequest("twitter"); // <1>
244+
request.type("tweet"); // <2>
245+
// end::put-mapping-request
246+
247+
// tag::put-mapping-request-source
248+
request.source(
249+
"{\n" +
250+
" \"tweet\": {\n" +
251+
" \"properties\": {\n" +
252+
" \"message\": {\n" +
253+
" \"type\": \"text\"\n" +
254+
" }\n" +
255+
" }\n" +
256+
" }\n" +
257+
"}", // <1>
258+
XContentType.JSON);
259+
// end::put-mapping-request-source
260+
261+
// tag::put-mapping-request-timeout
262+
request.timeout(TimeValue.timeValueMinutes(2)); // <1>
263+
request.timeout("2m"); // <2>
264+
// end::put-mapping-request-timeout
265+
// tag::put-mapping-request-masterTimeout
266+
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
267+
request.masterNodeTimeout("1m"); // <2>
268+
// end::put-mapping-request-masterTimeout
269+
270+
// tag::put-mapping-execute
271+
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
272+
// end::put-mapping-execute
273+
274+
// tag::put-mapping-response
275+
boolean acknowledged = putMappingResponse.isAcknowledged(); // <1>
276+
// end::put-mapping-response
277+
assertTrue(acknowledged);
278+
}
279+
}
280+
281+
public void testPutMappingAsync() throws Exception {
282+
final RestHighLevelClient client = highLevelClient();
283+
284+
{
285+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
286+
assertTrue(createIndexResponse.isAcknowledged());
287+
}
288+
289+
{
290+
PutMappingRequest request = new PutMappingRequest("twitter").type("tweet");
291+
// tag::put-mapping-execute-async
292+
client.indices().putMappingAsync(request, new ActionListener<PutMappingResponse>() {
293+
@Override
294+
public void onResponse(PutMappingResponse putMappingResponse) {
295+
// <1>
296+
}
297+
298+
@Override
299+
public void onFailure(Exception e) {
300+
// <2>
301+
}
302+
});
303+
// end::put-mapping-execute-async
304+
305+
assertBusy(() -> {
306+
// TODO Use Indices Exist API instead once it exists
307+
Response response = client.getLowLevelClient().performRequest("HEAD", "twitter");
308+
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode());
309+
});
310+
}
311+
}
312+
231313
public void testOpenIndex() throws IOException {
232314
RestHighLevelClient client = highLevelClient();
233315

docs/java-rest/high-level/apis/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ include::open_index.asciidoc[]
66

77
include::close_index.asciidoc[]
88

9+
include::putmapping.asciidoc[]
10+
911
include::_index.asciidoc[]
1012

1113
include::get.asciidoc[]

0 commit comments

Comments
 (0)