Skip to content

Commit f6c7a52

Browse files
committed
Merge branch '6.x' into ccr-6.x
* 6.x: HLRest: Allow caller to set per request options (#30490) Limit the scope of BouncyCastle dependency (#30959) Deprecates indexing and querying a context completion field without context (#31006) [DOCS] Clarify not all PKCS12 usable as truststores (#30750) Harmonize include_defaults tests (#30700) [DOCS] Update readme for testing x-pack code snippets (#30696) [Docs] Fix typo in Min Aggregation reference (#30899)
2 parents e05884b + 7acf0bb commit f6c7a52

File tree

263 files changed

+6113
-1768
lines changed

Some content is hidden

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

263 files changed

+6113
-1768
lines changed

CONTRIBUTING.md

+8
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ Before submitting your changes, run the test suite to make sure that nothing is
175175
./gradlew check
176176
```
177177

178+
If your changes affect only the documentation, run:
179+
180+
```sh
181+
./gradlew -p docs check
182+
```
183+
For more information about testing code examples in the documentation, see
184+
https://github.com/elastic/elasticsearch/blob/master/docs/README.asciidoc
185+
178186
### Project layout
179187

180188
This repository is split into many top level directories. The most important

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

+71-9
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ public final TasksClient tasks() {
279279
*
280280
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html">Bulk API on elastic.co</a>
281281
*/
282+
public final BulkResponse bulk(BulkRequest bulkRequest, RequestOptions options) throws IOException {
283+
return performRequestAndParseEntity(bulkRequest, RequestConverters::bulk, options, BulkResponse::fromXContent, emptySet());
284+
}
285+
286+
/**
287+
* Executes a bulk request using the Bulk API
288+
*
289+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html">Bulk API on elastic.co</a>
290+
* @deprecated Prefer {@link #bulk(BulkRequest, RequestOptions)}
291+
*/
292+
@Deprecated
282293
public final BulkResponse bulk(BulkRequest bulkRequest, Header... headers) throws IOException {
283294
return performRequestAndParseEntity(bulkRequest, RequestConverters::bulk, BulkResponse::fromXContent, emptySet(), headers);
284295
}
@@ -288,6 +299,17 @@ public final BulkResponse bulk(BulkRequest bulkRequest, Header... headers) throw
288299
*
289300
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html">Bulk API on elastic.co</a>
290301
*/
302+
public final void bulkAsync(BulkRequest bulkRequest, RequestOptions options, ActionListener<BulkResponse> listener) {
303+
performRequestAsyncAndParseEntity(bulkRequest, RequestConverters::bulk, options, BulkResponse::fromXContent, listener, emptySet());
304+
}
305+
306+
/**
307+
* Asynchronously executes a bulk request using the Bulk API
308+
*
309+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html">Bulk API on elastic.co</a>
310+
* @deprecated Prefer {@link #bulkAsync(BulkRequest, RequestOptions, ActionListener)}
311+
*/
312+
@Deprecated
291313
public final void bulkAsync(BulkRequest bulkRequest, ActionListener<BulkResponse> listener, Header... headers) {
292314
performRequestAsyncAndParseEntity(bulkRequest, RequestConverters::bulk, BulkResponse::fromXContent, listener, emptySet(), headers);
293315
}
@@ -584,23 +606,42 @@ public final void fieldCapsAsync(FieldCapabilitiesRequest fieldCapabilitiesReque
584606
FieldCapabilitiesResponse::fromXContent, listener, emptySet(), headers);
585607
}
586608

609+
@Deprecated
587610
protected final <Req extends ActionRequest, Resp> Resp performRequestAndParseEntity(Req request,
588611
CheckedFunction<Req, Request, IOException> requestConverter,
589612
CheckedFunction<XContentParser, Resp, IOException> entityParser,
590613
Set<Integer> ignores, Header... headers) throws IOException {
591614
return performRequest(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser), ignores, headers);
592615
}
593616

617+
protected final <Req extends ActionRequest, Resp> Resp performRequestAndParseEntity(Req request,
618+
CheckedFunction<Req, Request, IOException> requestConverter,
619+
RequestOptions options,
620+
CheckedFunction<XContentParser, Resp, IOException> entityParser,
621+
Set<Integer> ignores) throws IOException {
622+
return performRequest(request, requestConverter, options,
623+
response -> parseEntity(response.getEntity(), entityParser), ignores);
624+
}
625+
626+
@Deprecated
594627
protected final <Req extends ActionRequest, Resp> Resp performRequest(Req request,
595628
CheckedFunction<Req, Request, IOException> requestConverter,
596629
CheckedFunction<Response, Resp, IOException> responseConverter,
597630
Set<Integer> ignores, Header... headers) throws IOException {
631+
return performRequest(request, requestConverter, optionsForHeaders(headers), responseConverter, ignores);
632+
}
633+
634+
protected final <Req extends ActionRequest, Resp> Resp performRequest(Req request,
635+
CheckedFunction<Req, Request, IOException> requestConverter,
636+
RequestOptions options,
637+
CheckedFunction<Response, Resp, IOException> responseConverter,
638+
Set<Integer> ignores) throws IOException {
598639
ActionRequestValidationException validationException = request.validate();
599640
if (validationException != null) {
600641
throw validationException;
601642
}
602643
Request req = requestConverter.apply(request);
603-
addHeaders(req, headers);
644+
req.setOptions(options);
604645
Response response;
605646
try {
606647
response = client.performRequest(req);
@@ -626,6 +667,7 @@ protected final <Req extends ActionRequest, Resp> Resp performRequest(Req reques
626667
}
627668
}
628669

670+
@Deprecated
629671
protected final <Req extends ActionRequest, Resp> void performRequestAsyncAndParseEntity(Req request,
630672
CheckedFunction<Req, Request, IOException> requestConverter,
631673
CheckedFunction<XContentParser, Resp, IOException> entityParser,
@@ -634,10 +676,28 @@ protected final <Req extends ActionRequest, Resp> void performRequestAsyncAndPar
634676
listener, ignores, headers);
635677
}
636678

679+
protected final <Req extends ActionRequest, Resp> void performRequestAsyncAndParseEntity(Req request,
680+
CheckedFunction<Req, Request, IOException> requestConverter,
681+
RequestOptions options,
682+
CheckedFunction<XContentParser, Resp, IOException> entityParser,
683+
ActionListener<Resp> listener, Set<Integer> ignores) {
684+
performRequestAsync(request, requestConverter, options,
685+
response -> parseEntity(response.getEntity(), entityParser), listener, ignores);
686+
}
687+
688+
@Deprecated
637689
protected final <Req extends ActionRequest, Resp> void performRequestAsync(Req request,
638690
CheckedFunction<Req, Request, IOException> requestConverter,
639691
CheckedFunction<Response, Resp, IOException> responseConverter,
640692
ActionListener<Resp> listener, Set<Integer> ignores, Header... headers) {
693+
performRequestAsync(request, requestConverter, optionsForHeaders(headers), responseConverter, listener, ignores);
694+
}
695+
696+
protected final <Req extends ActionRequest, Resp> void performRequestAsync(Req request,
697+
CheckedFunction<Req, Request, IOException> requestConverter,
698+
RequestOptions options,
699+
CheckedFunction<Response, Resp, IOException> responseConverter,
700+
ActionListener<Resp> listener, Set<Integer> ignores) {
641701
ActionRequestValidationException validationException = request.validate();
642702
if (validationException != null) {
643703
listener.onFailure(validationException);
@@ -650,19 +710,12 @@ protected final <Req extends ActionRequest, Resp> void performRequestAsync(Req r
650710
listener.onFailure(e);
651711
return;
652712
}
653-
addHeaders(req, headers);
713+
req.setOptions(options);
654714

655715
ResponseListener responseListener = wrapResponseListener(responseConverter, listener, ignores);
656716
client.performRequestAsync(req, responseListener);
657717
}
658718

659-
private static void addHeaders(Request request, Header... headers) {
660-
Objects.requireNonNull(headers, "headers cannot be null");
661-
for (Header header : headers) {
662-
request.addHeader(header.getName(), header.getValue());
663-
}
664-
}
665-
666719
final <Resp> ResponseListener wrapResponseListener(CheckedFunction<Response, Resp, IOException> responseConverter,
667720
ActionListener<Resp> actionListener, Set<Integer> ignores) {
668721
return new ResponseListener() {
@@ -746,6 +799,15 @@ protected final <Resp> Resp parseEntity(final HttpEntity entity,
746799
}
747800
}
748801

802+
private static RequestOptions optionsForHeaders(Header[] headers) {
803+
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
804+
for (Header header : headers) {
805+
Objects.requireNonNull(header, "header cannot be null");
806+
options.addHeader(header.getName(), header.getValue());
807+
}
808+
return options.build();
809+
}
810+
749811
static boolean convertExistsResponse(Response response) {
750812
return response.getStatusLine().getStatusCode() == 200;
751813
}

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

+44-35
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.http.client.methods.HttpGet;
2727
import org.apache.http.entity.ByteArrayEntity;
2828
import org.apache.http.entity.ContentType;
29-
import org.apache.http.message.BasicHeader;
3029
import org.apache.http.message.BasicRequestLine;
3130
import org.apache.http.message.BasicStatusLine;
3231
import org.apache.lucene.util.BytesRef;
@@ -53,11 +52,13 @@
5352
import java.lang.reflect.Modifier;
5453
import java.util.Arrays;
5554
import java.util.Collections;
56-
import java.util.List;
55+
import java.util.Set;
56+
import java.util.TreeSet;
5757
import java.util.stream.Collectors;
5858

5959
import static java.util.Collections.emptySet;
60-
import static org.hamcrest.Matchers.containsInAnyOrder;
60+
import static org.hamcrest.Matchers.contains;
61+
import static org.hamcrest.Matchers.hasSize;
6162
import static org.mockito.Matchers.any;
6263
import static org.mockito.Mockito.doAnswer;
6364
import static org.mockito.Mockito.mock;
@@ -78,12 +79,12 @@ public void initClients() throws IOException {
7879
final RestClient restClient = mock(RestClient.class);
7980
restHighLevelClient = new CustomRestClient(restClient);
8081

81-
doAnswer(inv -> mockPerformRequest(((Request) inv.getArguments()[0]).getHeaders().iterator().next()))
82+
doAnswer(inv -> mockPerformRequest((Request) inv.getArguments()[0]))
8283
.when(restClient)
8384
.performRequest(any(Request.class));
8485

8586
doAnswer(inv -> mockPerformRequestAsync(
86-
((Request) inv.getArguments()[0]).getHeaders().iterator().next(),
87+
((Request) inv.getArguments()[0]),
8788
(ResponseListener) inv.getArguments()[1]))
8889
.when(restClient)
8990
.performRequestAsync(any(Request.class), any(ResponseListener.class));
@@ -92,26 +93,32 @@ public void initClients() throws IOException {
9293

9394
public void testCustomEndpoint() throws IOException {
9495
final MainRequest request = new MainRequest();
95-
final Header header = new BasicHeader("node_name", randomAlphaOfLengthBetween(1, 10));
96+
String nodeName = randomAlphaOfLengthBetween(1, 10);
9697

97-
MainResponse response = restHighLevelClient.custom(request, header);
98-
assertEquals(header.getValue(), response.getNodeName());
98+
MainResponse response = restHighLevelClient.custom(request, optionsForNodeName(nodeName));
99+
assertEquals(nodeName, response.getNodeName());
99100

100-
response = restHighLevelClient.customAndParse(request, header);
101-
assertEquals(header.getValue(), response.getNodeName());
101+
response = restHighLevelClient.customAndParse(request, optionsForNodeName(nodeName));
102+
assertEquals(nodeName, response.getNodeName());
102103
}
103104

104105
public void testCustomEndpointAsync() throws Exception {
105106
final MainRequest request = new MainRequest();
106-
final Header header = new BasicHeader("node_name", randomAlphaOfLengthBetween(1, 10));
107+
String nodeName = randomAlphaOfLengthBetween(1, 10);
107108

108109
PlainActionFuture<MainResponse> future = PlainActionFuture.newFuture();
109-
restHighLevelClient.customAsync(request, future, header);
110-
assertEquals(header.getValue(), future.get().getNodeName());
110+
restHighLevelClient.customAsync(request, optionsForNodeName(nodeName), future);
111+
assertEquals(nodeName, future.get().getNodeName());
111112

112113
future = PlainActionFuture.newFuture();
113-
restHighLevelClient.customAndParseAsync(request, future, header);
114-
assertEquals(header.getValue(), future.get().getNodeName());
114+
restHighLevelClient.customAndParseAsync(request, optionsForNodeName(nodeName), future);
115+
assertEquals(nodeName, future.get().getNodeName());
116+
}
117+
118+
private static RequestOptions optionsForNodeName(String nodeName) {
119+
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
120+
options.addHeader("node_name", nodeName);
121+
return options.build();
115122
}
116123

117124
/**
@@ -120,27 +127,27 @@ public void testCustomEndpointAsync() throws Exception {
120127
*/
121128
@SuppressForbidden(reason = "We're forced to uses Class#getDeclaredMethods() here because this test checks protected methods")
122129
public void testMethodsVisibility() throws ClassNotFoundException {
123-
final String[] methodNames = new String[]{"performRequest",
124-
"performRequestAsync",
130+
final String[] methodNames = new String[]{"parseEntity",
131+
"parseResponseException",
132+
"performRequest",
125133
"performRequestAndParseEntity",
126-
"performRequestAsyncAndParseEntity",
127-
"parseEntity",
128-
"parseResponseException"};
134+
"performRequestAsync",
135+
"performRequestAsyncAndParseEntity"};
129136

130-
final List<String> protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods())
137+
final Set<String> protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods())
131138
.filter(method -> Modifier.isProtected(method.getModifiers()))
132139
.map(Method::getName)
133-
.collect(Collectors.toList());
140+
.collect(Collectors.toCollection(TreeSet::new));
134141

135-
assertThat(protectedMethods, containsInAnyOrder(methodNames));
142+
assertThat(protectedMethods, contains(methodNames));
136143
}
137144

138145
/**
139-
* Mocks the asynchronous request execution by calling the {@link #mockPerformRequest(Header)} method.
146+
* Mocks the asynchronous request execution by calling the {@link #mockPerformRequest(Request)} method.
140147
*/
141-
private Void mockPerformRequestAsync(Header httpHeader, ResponseListener responseListener) {
148+
private Void mockPerformRequestAsync(Request request, ResponseListener responseListener) {
142149
try {
143-
responseListener.onSuccess(mockPerformRequest(httpHeader));
150+
responseListener.onSuccess(mockPerformRequest(request));
144151
} catch (IOException e) {
145152
responseListener.onFailure(e);
146153
}
@@ -150,7 +157,9 @@ private Void mockPerformRequestAsync(Header httpHeader, ResponseListener respons
150157
/**
151158
* Mocks the synchronous request execution like if it was executed by Elasticsearch.
152159
*/
153-
private Response mockPerformRequest(Header httpHeader) throws IOException {
160+
private Response mockPerformRequest(Request request) throws IOException {
161+
assertThat(request.getOptions().getHeaders(), hasSize(1));
162+
Header httpHeader = request.getOptions().getHeaders().get(0);
154163
final Response mockResponse = mock(Response.class);
155164
when(mockResponse.getHost()).thenReturn(new HttpHost("localhost", 9200));
156165

@@ -176,20 +185,20 @@ private CustomRestClient(RestClient restClient) {
176185
super(restClient, RestClient::close, Collections.emptyList());
177186
}
178187

179-
MainResponse custom(MainRequest mainRequest, Header... headers) throws IOException {
180-
return performRequest(mainRequest, this::toRequest, this::toResponse, emptySet(), headers);
188+
MainResponse custom(MainRequest mainRequest, RequestOptions options) throws IOException {
189+
return performRequest(mainRequest, this::toRequest, options, this::toResponse, emptySet());
181190
}
182191

183-
MainResponse customAndParse(MainRequest mainRequest, Header... headers) throws IOException {
184-
return performRequestAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, emptySet(), headers);
192+
MainResponse customAndParse(MainRequest mainRequest, RequestOptions options) throws IOException {
193+
return performRequestAndParseEntity(mainRequest, this::toRequest, options, MainResponse::fromXContent, emptySet());
185194
}
186195

187-
void customAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) {
188-
performRequestAsync(mainRequest, this::toRequest, this::toResponse, listener, emptySet(), headers);
196+
void customAsync(MainRequest mainRequest, RequestOptions options, ActionListener<MainResponse> listener) {
197+
performRequestAsync(mainRequest, this::toRequest, options, this::toResponse, listener, emptySet());
189198
}
190199

191-
void customAndParseAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) {
192-
performRequestAsyncAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, listener, emptySet(), headers);
200+
void customAndParseAsync(MainRequest mainRequest, RequestOptions options, ActionListener<MainResponse> listener) {
201+
performRequestAsyncAndParseEntity(mainRequest, this::toRequest, options, MainResponse::fromXContent, listener, emptySet());
193202
}
194203

195204
Request toRequest(MainRequest mainRequest) throws IOException {

0 commit comments

Comments
 (0)