Skip to content

Commit 5bf32af

Browse files
committed
Merge remote-tracking branch 'elastic/6.x' into ccr-6.x
* elastic/6.x: [Rollup] Disallow index patterns that match the rollup index (#30491) Revert "Fixing MixedClusterClientYamlTestSuiteIT" Fixing MixedClusterClientYamlTestSuiteIT Add get mappings support to high-level rest client (#30889) [DOCS] Fixes security example (#31082) Allow terms query in _rollup_search (#30973)
2 parents addf09a + 3aeff08 commit 5bf32af

File tree

21 files changed

+819
-128
lines changed

21 files changed

+819
-128
lines changed

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.Header;
23+
import org.elasticsearch.action.Action;
2324
import org.elasticsearch.action.ActionListener;
2425
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
2526
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
@@ -38,6 +39,8 @@
3839
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
3940
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4041
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
42+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
43+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
4144
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4245
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
4346
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
@@ -134,11 +137,34 @@ public PutMappingResponse putMapping(PutMappingRequest putMappingRequest, Header
134137
* Put Mapping API on elastic.co</a>
135138
*/
136139
public void putMappingAsync(PutMappingRequest putMappingRequest, ActionListener<PutMappingResponse> listener,
137-
Header... headers) {
140+
Header... headers) {
138141
restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, RequestConverters::putMapping,
139142
PutMappingResponse::fromXContent, listener, emptySet(), headers);
140143
}
141144

145+
/**
146+
* Retrieves the mappings on an index or indices using the Get Mapping API
147+
* <p>
148+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
149+
* Get Mapping API on elastic.co</a>
150+
*/
151+
public GetMappingsResponse getMappings(GetMappingsRequest getMappingsRequest, Header... headers) throws IOException {
152+
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, RequestConverters::getMappings,
153+
GetMappingsResponse::fromXContent, emptySet(), headers);
154+
}
155+
156+
/**
157+
* Asynchronously retrieves the mappings on an index on indices using the Get Mapping API
158+
* <p>
159+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
160+
* Get Mapping API on elastic.co</a>
161+
*/
162+
public void getMappingsAsync(GetMappingsRequest getMappingsRequest, ActionListener<GetMappingsResponse> listener,
163+
Header... headers) {
164+
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, RequestConverters::getMappings,
165+
GetMappingsResponse::fromXContent, listener, emptySet(), headers);
166+
}
167+
142168
/**
143169
* Updates aliases using the Index Aliases API
144170
* <p>

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

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
4646
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4747
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
48+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
4849
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4950
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
5051
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@@ -198,6 +199,19 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
198199
return request;
199200
}
200201

202+
static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException {
203+
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
204+
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();
205+
206+
Request request = new Request(HttpGet.METHOD_NAME, endpoint(indices, "_mapping", types));
207+
208+
Params parameters = new Params(request);
209+
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
210+
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
211+
parameters.withLocal(getMappingsRequest.local());
212+
return request;
213+
}
214+
201215
static Request refresh(RefreshRequest refreshRequest) {
202216
String[] indices = refreshRequest.indices() == null ? Strings.EMPTY_ARRAY : refreshRequest.indices();
203217
Request request = new Request(HttpPost.METHOD_NAME, endpoint(indices, "_refresh"));

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

+39
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4343
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4444
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
45+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
46+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
4547
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4648
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
4749
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
@@ -80,6 +82,7 @@
8082
import java.io.IOException;
8183
import java.util.Arrays;
8284
import java.util.Collections;
85+
import java.util.HashMap;
8386
import java.util.Map;
8487

8588
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
@@ -329,6 +332,42 @@ public void testPutMapping() throws IOException {
329332
}
330333
}
331334

335+
public void testGetMapping() throws IOException {
336+
String indexName = "test";
337+
createIndex(indexName, Settings.EMPTY);
338+
339+
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
340+
putMappingRequest.type("_doc");
341+
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
342+
mappingBuilder.startObject().startObject("properties").startObject("field");
343+
mappingBuilder.field("type", "text");
344+
mappingBuilder.endObject().endObject().endObject();
345+
putMappingRequest.source(mappingBuilder);
346+
347+
PutMappingResponse putMappingResponse =
348+
execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
349+
assertTrue(putMappingResponse.isAcknowledged());
350+
351+
Map<String, Object> getIndexResponse = getAsMap(indexName);
352+
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings._doc.properties.field.type", getIndexResponse));
353+
354+
GetMappingsRequest request = new GetMappingsRequest()
355+
.indices(indexName)
356+
.types("_doc");
357+
358+
GetMappingsResponse getMappingsResponse =
359+
execute(request, highLevelClient().indices()::getMappings, highLevelClient().indices()::getMappingsAsync);
360+
361+
Map<String, Object> mappings = getMappingsResponse.getMappings().get(indexName).get("_doc").sourceAsMap();
362+
Map<String, String> type = new HashMap<>();
363+
type.put("type", "text");
364+
Map<String, Object> field = new HashMap<>();
365+
field.put("field", type);
366+
Map<String, Object> expected = new HashMap<>();
367+
expected.put("properties", field);
368+
assertThat(mappings, equalTo(expected));
369+
}
370+
332371
public void testDeleteIndex() throws IOException {
333372
{
334373
// Delete index if exists

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

+42
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
4848
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4949
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
50+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
5051
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
5152
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
5253
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@@ -413,6 +414,47 @@ public void testPutMapping() throws IOException {
413414
assertToXContentBody(putMappingRequest, request.getEntity());
414415
}
415416

417+
public void testGetMapping() throws IOException {
418+
GetMappingsRequest getMappingRequest = new GetMappingsRequest();
419+
420+
String[] indices = Strings.EMPTY_ARRAY;
421+
if (randomBoolean()) {
422+
indices = randomIndicesNames(0, 5);
423+
getMappingRequest.indices(indices);
424+
} else if (randomBoolean()) {
425+
getMappingRequest.indices((String[]) null);
426+
}
427+
428+
String type = null;
429+
if (randomBoolean()) {
430+
type = randomAlphaOfLengthBetween(3, 10);
431+
getMappingRequest.types(type);
432+
} else if (randomBoolean()) {
433+
getMappingRequest.types((String[]) null);
434+
}
435+
436+
Map<String, String> expectedParams = new HashMap<>();
437+
438+
setRandomIndicesOptions(getMappingRequest::indicesOptions, getMappingRequest::indicesOptions, expectedParams);
439+
setRandomMasterTimeout(getMappingRequest, expectedParams);
440+
setRandomLocal(getMappingRequest, expectedParams);
441+
442+
Request request = RequestConverters.getMappings(getMappingRequest);
443+
StringJoiner endpoint = new StringJoiner("/", "/", "");
444+
String index = String.join(",", indices);
445+
if (Strings.hasLength(index)) {
446+
endpoint.add(index);
447+
}
448+
endpoint.add("_mapping");
449+
if (type != null) {
450+
endpoint.add(type);
451+
}
452+
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
453+
454+
assertThat(expectedParams, equalTo(request.getParameters()));
455+
assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
456+
}
457+
416458
public void testDeleteIndex() {
417459
String[] indices = randomIndicesNames(0, 5);
418460
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices);

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

+143-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4242
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4343
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
44+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
45+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
4446
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
4547
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
4648
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
@@ -64,6 +66,8 @@
6466
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
6567
import org.elasticsearch.client.RestHighLevelClient;
6668
import org.elasticsearch.client.SyncedFlushResponse;
69+
import org.elasticsearch.cluster.metadata.MappingMetaData;
70+
import org.elasticsearch.common.collect.ImmutableOpenMap;
6771
import org.elasticsearch.common.settings.Settings;
6872
import org.elasticsearch.common.unit.ByteSizeUnit;
6973
import org.elasticsearch.common.unit.ByteSizeValue;
@@ -81,6 +85,8 @@
8185
import java.util.concurrent.CountDownLatch;
8286
import java.util.concurrent.TimeUnit;
8387

88+
import static org.hamcrest.Matchers.equalTo;
89+
8490
/**
8591
* This class is used to generate the Java Indices API documentation.
8692
* You need to wrap your code between two tags like:
@@ -532,17 +538,17 @@ public void testPutMappingAsync() throws Exception {
532538

533539
// tag::put-mapping-execute-listener
534540
ActionListener<PutMappingResponse> listener =
535-
new ActionListener<PutMappingResponse>() {
536-
@Override
537-
public void onResponse(PutMappingResponse putMappingResponse) {
538-
// <1>
539-
}
541+
new ActionListener<PutMappingResponse>() {
542+
@Override
543+
public void onResponse(PutMappingResponse putMappingResponse) {
544+
// <1>
545+
}
540546

541-
@Override
542-
public void onFailure(Exception e) {
543-
// <2>
544-
}
545-
};
547+
@Override
548+
public void onFailure(Exception e) {
549+
// <2>
550+
}
551+
};
546552
// end::put-mapping-execute-listener
547553

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

566+
public void testGetMapping() throws IOException {
567+
RestHighLevelClient client = highLevelClient();
568+
569+
{
570+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
571+
assertTrue(createIndexResponse.isAcknowledged());
572+
PutMappingRequest request = new PutMappingRequest("twitter");
573+
request.type("tweet");
574+
request.source(
575+
"{\n" +
576+
" \"properties\": {\n" +
577+
" \"message\": {\n" +
578+
" \"type\": \"text\"\n" +
579+
" }\n" +
580+
" }\n" +
581+
"}", // <1>
582+
XContentType.JSON);
583+
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
584+
assertTrue(putMappingResponse.isAcknowledged());
585+
}
586+
587+
{
588+
// tag::get-mapping-request
589+
GetMappingsRequest request = new GetMappingsRequest(); // <1>
590+
request.indices("twitter"); // <2>
591+
request.types("tweet"); // <3>
592+
// end::get-mapping-request
593+
594+
// tag::get-mapping-request-masterTimeout
595+
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
596+
request.masterNodeTimeout("1m"); // <2>
597+
// end::get-mapping-request-masterTimeout
598+
599+
// tag::get-mapping-request-indicesOptions
600+
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
601+
// end::get-mapping-request-indicesOptions
602+
603+
// tag::get-mapping-execute
604+
GetMappingsResponse getMappingResponse = client.indices().getMappings(request);
605+
// end::get-mapping-execute
606+
607+
// tag::get-mapping-response
608+
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = getMappingResponse.mappings(); // <1>
609+
MappingMetaData typeMapping = allMappings.get("twitter").get("tweet"); // <2>
610+
Map<String, Object> tweetMapping = typeMapping.sourceAsMap(); // <3>
611+
// end::get-mapping-response
612+
613+
Map<String, String> type = new HashMap<>();
614+
type.put("type", "text");
615+
Map<String, Object> field = new HashMap<>();
616+
field.put("message", type);
617+
Map<String, Object> expected = new HashMap<>();
618+
expected.put("properties", field);
619+
assertThat(tweetMapping, equalTo(expected));
620+
}
621+
}
622+
623+
public void testGetMappingAsync() throws Exception {
624+
final RestHighLevelClient client = highLevelClient();
625+
626+
{
627+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"));
628+
assertTrue(createIndexResponse.isAcknowledged());
629+
PutMappingRequest request = new PutMappingRequest("twitter");
630+
request.type("tweet");
631+
request.source(
632+
"{\n" +
633+
" \"properties\": {\n" +
634+
" \"message\": {\n" +
635+
" \"type\": \"text\"\n" +
636+
" }\n" +
637+
" }\n" +
638+
"}", // <1>
639+
XContentType.JSON);
640+
PutMappingResponse putMappingResponse = client.indices().putMapping(request);
641+
assertTrue(putMappingResponse.isAcknowledged());
642+
}
643+
644+
{
645+
GetMappingsRequest request = new GetMappingsRequest();
646+
request.indices("twitter");
647+
request.types("tweet");
648+
649+
// tag::get-mapping-execute-listener
650+
ActionListener<GetMappingsResponse> listener =
651+
new ActionListener<GetMappingsResponse>() {
652+
@Override
653+
public void onResponse(GetMappingsResponse putMappingResponse) {
654+
// <1>
655+
}
656+
657+
@Override
658+
public void onFailure(Exception e) {
659+
// <2>
660+
}
661+
};
662+
// end::get-mapping-execute-listener
663+
664+
// Replace the empty listener by a blocking listener in test
665+
final CountDownLatch latch = new CountDownLatch(1);
666+
final ActionListener<GetMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
667+
listener = ActionListener.wrap(r -> {
668+
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = r.mappings();
669+
MappingMetaData typeMapping = allMappings.get("twitter").get("tweet");
670+
Map<String, Object> tweetMapping = typeMapping.sourceAsMap();
671+
672+
Map<String, String> type = new HashMap<>();
673+
type.put("type", "text");
674+
Map<String, Object> field = new HashMap<>();
675+
field.put("message", type);
676+
Map<String, Object> expected = new HashMap<>();
677+
expected.put("properties", field);
678+
assertThat(tweetMapping, equalTo(expected));
679+
latchListener.onResponse(r);
680+
}, e -> {
681+
latchListener.onFailure(e);
682+
fail("should not fail");
683+
});
684+
685+
// tag::get-mapping-execute-async
686+
client.indices().getMappingsAsync(request, listener); // <1>
687+
// end::get-mapping-execute-async
688+
689+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
690+
}
691+
}
692+
560693
public void testOpenIndex() throws Exception {
561694
RestHighLevelClient client = highLevelClient();
562695

0 commit comments

Comments
 (0)