Skip to content

Commit 646e1c0

Browse files
committed
Merge branch 'master' into ccr
* master: Add get mappings support to high-level rest client (#30889) Fix index prefixes to work with span_multi (#31066) [DOCS] Removes redundant authorization pages [DOCS] Re-adds custom realm Change ObjectParser exception (#31030) Upgrade to Lucene-7.4.0-snapshot-0a7c3f462f (#31073)
2 parents 6ee6404 + b22a055 commit 646e1c0

File tree

90 files changed

+1145
-565
lines changed

Some content is hidden

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

90 files changed

+1145
-565
lines changed

buildSrc/version.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
elasticsearch = 7.0.0-alpha1
2-
lucene = 7.4.0-snapshot-1cbadda4d3
2+
lucene = 7.4.0-snapshot-0a7c3f462f
33

44
# optional dependencies
55
spatial4j = 0.7

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;
@@ -195,6 +196,19 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
195196
return request;
196197
}
197198

199+
static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException {
200+
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
201+
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();
202+
203+
Request request = new Request(HttpGet.METHOD_NAME, endpoint(indices, "_mapping", types));
204+
205+
Params parameters = new Params(request);
206+
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
207+
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
208+
parameters.withLocal(getMappingsRequest.local());
209+
return request;
210+
}
211+
198212
static Request refresh(RefreshRequest refreshRequest) {
199213
String[] indices = refreshRequest.indices() == null ? Strings.EMPTY_ARRAY : refreshRequest.indices();
200214
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;
@@ -79,6 +81,7 @@
7981
import java.io.IOException;
8082
import java.util.Arrays;
8183
import java.util.Collections;
84+
import java.util.HashMap;
8285
import java.util.Map;
8386

8487
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
@@ -328,6 +331,42 @@ public void testPutMapping() throws IOException {
328331
}
329332
}
330333

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

407+
public void testGetMapping() throws IOException {
408+
GetMappingsRequest getMappingRequest = new GetMappingsRequest();
409+
410+
String[] indices = Strings.EMPTY_ARRAY;
411+
if (randomBoolean()) {
412+
indices = randomIndicesNames(0, 5);
413+
getMappingRequest.indices(indices);
414+
} else if (randomBoolean()) {
415+
getMappingRequest.indices((String[]) null);
416+
}
417+
418+
String type = null;
419+
if (randomBoolean()) {
420+
type = randomAlphaOfLengthBetween(3, 10);
421+
getMappingRequest.types(type);
422+
} else if (randomBoolean()) {
423+
getMappingRequest.types((String[]) null);
424+
}
425+
426+
Map<String, String> expectedParams = new HashMap<>();
427+
428+
setRandomIndicesOptions(getMappingRequest::indicesOptions, getMappingRequest::indicesOptions, expectedParams);
429+
setRandomMasterTimeout(getMappingRequest, expectedParams);
430+
setRandomLocal(getMappingRequest, expectedParams);
431+
432+
Request request = RequestConverters.getMappings(getMappingRequest);
433+
StringJoiner endpoint = new StringJoiner("/", "/", "");
434+
String index = String.join(",", indices);
435+
if (Strings.hasLength(index)) {
436+
endpoint.add(index);
437+
}
438+
endpoint.add("_mapping");
439+
if (type != null) {
440+
endpoint.add(type);
441+
}
442+
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
443+
444+
assertThat(expectedParams, equalTo(request.getParameters()));
445+
assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
446+
}
447+
406448
public void testDeleteIndex() {
407449
String[] indices = randomIndicesNames(0, 5);
408450
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)