Skip to content

Commit 08bb6fa

Browse files
committed
Merge branch 'master' into primary-context
* master: (53 commits) Log checkout so SHA is known Add link to community Rust Client (elastic#22897) "shard started" should show index and shard ID (elastic#25157) await fix testWithRandomException Change BWC versions on create index response Return the index name on a create index response Remove incorrect bwc branch logic from master Correctly format arrays in output [Test] Extending parsing checks for SearchResponse (elastic#25148) Scripting: Change keys for inline/stored scripts to source/id (elastic#25127) [Test] Add test for custom requests in High Level Rest Client (elastic#25106) nested: In case of a single type the _id field should be added to the nested document instead of _uid field. `type` and `id` are lost upon serialization of `Translog.Delete`. (elastic#24586) fix highlighting docs Fix NPE in token_count datatype with null value (elastic#25046) Remove the postings highlighter and make unified the default highlighter choice (elastic#25028) [Test] Adding test for parsing SearchShardFailure leniently (elastic#25144) Fix typo in shards.asciidoc (elastic#25143) List Hibernate Search (elastic#25145) [DOCS] update maxRetryTimeout in java REST client usage page ...
2 parents 76162e4 + a7a3af6 commit 08bb6fa

File tree

306 files changed

+5615
-3769
lines changed

Some content is hidden

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

306 files changed

+5615
-3769
lines changed

buildSrc/src/main/resources/checkstyle_suppressions.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<!-- Hopefully temporary suppression of LineLength on files that don't pass it. We should remove these when we the
1717
files start to pass. -->
1818
<suppress files="client[/\\]rest[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]client[/\\]HeapBufferedAsyncResponseConsumerTests.java" checks="LineLength" />
19-
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]apache[/\\]lucene[/\\]search[/\\]postingshighlight[/\\]CustomPostingsHighlighter.java" checks="LineLength" />
2019
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]apache[/\\]lucene[/\\]search[/\\]vectorhighlight[/\\]CustomFieldQuery.java" checks="LineLength" />
2120
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]Action.java" checks="LineLength" />
2221
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]ActionRequestBuilder.java" checks="LineLength" />
@@ -428,7 +427,6 @@
428427
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]threadpool[/\\]ThreadPool.java" checks="LineLength" />
429428
<suppress files="core[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]tribe[/\\]TribeService.java" checks="LineLength" />
430429
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]apache[/\\]lucene[/\\]queries[/\\]BlendedTermQueryTests.java" checks="LineLength" />
431-
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]apache[/\\]lucene[/\\]search[/\\]postingshighlight[/\\]CustomPostingsHighlighterTests.java" checks="LineLength" />
432430
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]VersionTests.java" checks="LineLength" />
433431
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]RejectionActionIT.java" checks="LineLength" />
434432
<suppress files="core[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]action[/\\]admin[/\\]HotThreadsIT.java" checks="LineLength" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ public void clearScrollAsync(ClearScrollRequest clearScrollRequest, ActionListen
371371
listener, emptySet(), headers);
372372
}
373373

374-
private <Req extends ActionRequest, Resp> Resp performRequestAndParseEntity(Req request,
374+
protected <Req extends ActionRequest, Resp> Resp performRequestAndParseEntity(Req request,
375375
CheckedFunction<Req, Request, IOException> requestConverter,
376376
CheckedFunction<XContentParser, Resp, IOException> entityParser,
377377
Set<Integer> ignores, Header... headers) throws IOException {
378378
return performRequest(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser), ignores, headers);
379379
}
380380

381-
<Req extends ActionRequest, Resp> Resp performRequest(Req request,
381+
protected <Req extends ActionRequest, Resp> Resp performRequest(Req request,
382382
CheckedFunction<Req, Request, IOException> requestConverter,
383383
CheckedFunction<Response, Resp, IOException> responseConverter,
384384
Set<Integer> ignores, Header... headers) throws IOException {
@@ -408,15 +408,15 @@ <Req extends ActionRequest, Resp> Resp performRequest(Req request,
408408
}
409409
}
410410

411-
private <Req extends ActionRequest, Resp> void performRequestAsyncAndParseEntity(Req request,
411+
protected <Req extends ActionRequest, Resp> void performRequestAsyncAndParseEntity(Req request,
412412
CheckedFunction<Req, Request, IOException> requestConverter,
413413
CheckedFunction<XContentParser, Resp, IOException> entityParser,
414414
ActionListener<Resp> listener, Set<Integer> ignores, Header... headers) {
415415
performRequestAsync(request, requestConverter, (response) -> parseEntity(response.getEntity(), entityParser),
416416
listener, ignores, headers);
417417
}
418418

419-
<Req extends ActionRequest, Resp> void performRequestAsync(Req request,
419+
protected <Req extends ActionRequest, Resp> void performRequestAsync(Req request,
420420
CheckedFunction<Req, Request, IOException> requestConverter,
421421
CheckedFunction<Response, Resp, IOException> responseConverter,
422422
ActionListener<Resp> listener, Set<Integer> ignores, Header... headers) {
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.Header;
23+
import org.apache.http.HttpEntity;
24+
import org.apache.http.HttpHost;
25+
import org.apache.http.HttpResponse;
26+
import org.apache.http.ProtocolVersion;
27+
import org.apache.http.RequestLine;
28+
import org.apache.http.client.methods.HttpGet;
29+
import org.apache.http.entity.ByteArrayEntity;
30+
import org.apache.http.entity.ContentType;
31+
import org.apache.http.message.BasicHeader;
32+
import org.apache.http.message.BasicHttpResponse;
33+
import org.apache.http.message.BasicRequestLine;
34+
import org.apache.http.message.BasicStatusLine;
35+
import org.apache.lucene.util.BytesRef;
36+
import org.elasticsearch.Build;
37+
import org.elasticsearch.Version;
38+
import org.elasticsearch.action.ActionListener;
39+
import org.elasticsearch.action.main.MainRequest;
40+
import org.elasticsearch.action.main.MainResponse;
41+
import org.elasticsearch.cluster.ClusterName;
42+
import org.elasticsearch.common.SuppressForbidden;
43+
import org.elasticsearch.common.xcontent.XContentHelper;
44+
import org.elasticsearch.common.xcontent.XContentType;
45+
import org.elasticsearch.test.ESTestCase;
46+
import org.junit.Before;
47+
48+
import java.io.IOException;
49+
import java.lang.reflect.Method;
50+
import java.lang.reflect.Modifier;
51+
52+
import static java.util.Collections.emptyMap;
53+
import static java.util.Collections.emptySet;
54+
import static org.elasticsearch.client.ESRestHighLevelClientTestCase.execute;
55+
import static org.mockito.Matchers.any;
56+
import static org.mockito.Matchers.anyMapOf;
57+
import static org.mockito.Matchers.anyObject;
58+
import static org.mockito.Matchers.anyVararg;
59+
import static org.mockito.Matchers.eq;
60+
import static org.mockito.Mockito.doAnswer;
61+
import static org.mockito.Mockito.mock;
62+
63+
/**
64+
* Test and demonstrates how {@link RestHighLevelClient} can be extended to support custom endpoints.
65+
*/
66+
public class CustomRestHighLevelClientTests extends ESTestCase {
67+
68+
private static final String ENDPOINT = "/_custom";
69+
70+
private CustomRestClient restHighLevelClient;
71+
72+
@Before
73+
@SuppressWarnings("unchecked")
74+
public void initClients() throws IOException {
75+
if (restHighLevelClient == null) {
76+
final RestClient restClient = mock(RestClient.class);
77+
restHighLevelClient = new CustomRestClient(restClient);
78+
79+
doAnswer(mock -> mockPerformRequest((Header) mock.getArguments()[4]))
80+
.when(restClient)
81+
.performRequest(eq(HttpGet.METHOD_NAME), eq(ENDPOINT), anyMapOf(String.class, String.class), anyObject(), anyVararg());
82+
83+
doAnswer(mock -> mockPerformRequestAsync((Header) mock.getArguments()[5], (ResponseListener) mock.getArguments()[4]))
84+
.when(restClient)
85+
.performRequestAsync(eq(HttpGet.METHOD_NAME), eq(ENDPOINT), anyMapOf(String.class, String.class),
86+
any(HttpEntity.class), any(ResponseListener.class), anyVararg());
87+
}
88+
}
89+
90+
public void testCustomEndpoint() throws IOException {
91+
final MainRequest request = new MainRequest();
92+
final Header header = new BasicHeader("node_name", randomAlphaOfLengthBetween(1, 10));
93+
94+
MainResponse response = execute(request, restHighLevelClient::custom, restHighLevelClient::customAsync, header);
95+
assertEquals(header.getValue(), response.getNodeName());
96+
97+
response = execute(request, restHighLevelClient::customAndParse, restHighLevelClient::customAndParseAsync, header);
98+
assertEquals(header.getValue(), response.getNodeName());
99+
}
100+
101+
/**
102+
* The {@link RestHighLevelClient} must declare the following execution methods using the <code>protected</code> modifier
103+
* so that they can be used by subclasses to implement custom logic.
104+
*/
105+
@SuppressForbidden(reason = "We're forced to uses Class#getDeclaredMethods() here because this test checks protected methods")
106+
public void testMethodsVisibility() throws ClassNotFoundException {
107+
String[] methodNames = new String[]{"performRequest", "performRequestAndParseEntity", "performRequestAsync",
108+
"performRequestAsyncAndParseEntity"};
109+
for (String methodName : methodNames) {
110+
boolean found = false;
111+
for (Method method : RestHighLevelClient.class.getDeclaredMethods()) {
112+
if (method.getName().equals(methodName)) {
113+
assertTrue("Method " + methodName + " must be protected", Modifier.isProtected(method.getModifiers()));
114+
found = true;
115+
}
116+
}
117+
assertTrue("Failed to find method " + methodName, found);
118+
}
119+
}
120+
121+
/**
122+
* Mocks the asynchronous request execution by calling the {@link #mockPerformRequest(Header)} method.
123+
*/
124+
private Void mockPerformRequestAsync(Header httpHeader, ResponseListener responseListener) {
125+
try {
126+
responseListener.onSuccess(mockPerformRequest(httpHeader));
127+
} catch (IOException e) {
128+
responseListener.onFailure(e);
129+
}
130+
return null;
131+
}
132+
133+
/**
134+
* Mocks the synchronous request execution like if it was executed by Elasticsearch.
135+
*/
136+
private Response mockPerformRequest(Header httpHeader) throws IOException {
137+
ProtocolVersion protocol = new ProtocolVersion("HTTP", 1, 1);
138+
HttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(protocol, 200, "OK"));
139+
140+
MainResponse response = new MainResponse(httpHeader.getValue(), Version.CURRENT, ClusterName.DEFAULT, "_na", Build.CURRENT, true);
141+
BytesRef bytesRef = XContentHelper.toXContent(response, XContentType.JSON, false).toBytesRef();
142+
httpResponse.setEntity(new ByteArrayEntity(bytesRef.bytes, ContentType.APPLICATION_JSON));
143+
144+
RequestLine requestLine = new BasicRequestLine(HttpGet.METHOD_NAME, ENDPOINT, protocol);
145+
return new Response(requestLine, new HttpHost("localhost", 9200), httpResponse);
146+
}
147+
148+
/**
149+
* A custom high level client that provides custom methods to execute a request and get its associate response back.
150+
*/
151+
static class CustomRestClient extends RestHighLevelClient {
152+
153+
private CustomRestClient(RestClient restClient) {
154+
super(restClient);
155+
}
156+
157+
MainResponse custom(MainRequest mainRequest, Header... headers) throws IOException {
158+
return performRequest(mainRequest, this::toRequest, this::toResponse, emptySet(), headers);
159+
}
160+
161+
MainResponse customAndParse(MainRequest mainRequest, Header... headers) throws IOException {
162+
return performRequestAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, emptySet(), headers);
163+
}
164+
165+
void customAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) {
166+
performRequestAsync(mainRequest, this::toRequest, this::toResponse, listener, emptySet(), headers);
167+
}
168+
169+
void customAndParseAsync(MainRequest mainRequest, ActionListener<MainResponse> listener, Header... headers) {
170+
performRequestAsyncAndParseEntity(mainRequest, this::toRequest, MainResponse::fromXContent, listener, emptySet(), headers);
171+
}
172+
173+
Request toRequest(MainRequest mainRequest) throws IOException {
174+
return new Request(HttpGet.METHOD_NAME, ENDPOINT, emptyMap(), null);
175+
}
176+
177+
MainResponse toResponse(Response response) throws IOException {
178+
return parseEntity(response.getEntity(), MainResponse::fromXContent);
179+
}
180+
}
181+
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ public void testSearchWithRangeAgg() throws IOException {
174174
assertSearchHeader(searchResponse);
175175
assertNull(searchResponse.getSuggest());
176176
assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
177-
assertThat(searchResponse.getTook().nanos(), greaterThan(0L));
178177
assertEquals(5, searchResponse.getHits().totalHits);
179178
assertEquals(0, searchResponse.getHits().getHits().length);
180179
assertEquals(0f, searchResponse.getHits().getMaxScore(), 0f);
@@ -257,7 +256,6 @@ public void testSearchWithMatrixStats() throws IOException {
257256
assertSearchHeader(searchResponse);
258257
assertNull(searchResponse.getSuggest());
259258
assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
260-
assertThat(searchResponse.getTook().nanos(), greaterThan(0L));
261259
assertEquals(5, searchResponse.getHits().totalHits);
262260
assertEquals(0, searchResponse.getHits().getHits().length);
263261
assertEquals(0f, searchResponse.getHits().getMaxScore(), 0f);
@@ -337,7 +335,6 @@ public void testSearchWithParentJoin() throws IOException {
337335
assertSearchHeader(searchResponse);
338336
assertNull(searchResponse.getSuggest());
339337
assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
340-
assertThat(searchResponse.getTook().nanos(), greaterThan(0L));
341338
assertEquals(3, searchResponse.getHits().totalHits);
342339
assertEquals(0, searchResponse.getHits().getHits().length);
343340
assertEquals(0f, searchResponse.getHits().getMaxScore(), 0f);

core/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ dependencies {
8181
compile "com.vividsolutions:jts:${versions.jts}", optional
8282

8383
// logging
84-
compile "org.apache.logging.log4j:log4j-api:${versions.log4j}", optional
84+
compile "org.apache.logging.log4j:log4j-api:${versions.log4j}"
8585
compile "org.apache.logging.log4j:log4j-core:${versions.log4j}", optional
8686
// to bridge dependencies that are still on Log4j 1 to Log4j 2
8787
compile "org.apache.logging.log4j:log4j-1.2-api:${versions.log4j}", optional

core/src/main/java/org/apache/lucene/queries/BlendedTermQuery.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -296,27 +296,6 @@ public int hashCode() {
296296
return Objects.hash(classHash(), Arrays.hashCode(equalsTerms()));
297297
}
298298

299-
public static BlendedTermQuery booleanBlendedQuery(Term[] terms) {
300-
return booleanBlendedQuery(terms, null);
301-
}
302-
303-
public static BlendedTermQuery booleanBlendedQuery(Term[] terms, final float[] boosts) {
304-
return new BlendedTermQuery(terms, boosts) {
305-
@Override
306-
protected Query topLevelQuery(Term[] terms, TermContext[] ctx, int[] docFreqs, int maxDoc) {
307-
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
308-
for (int i = 0; i < terms.length; i++) {
309-
Query query = new TermQuery(terms[i], ctx[i]);
310-
if (boosts != null && boosts[i] != 1f) {
311-
query = new BoostQuery(query, boosts[i]);
312-
}
313-
booleanQueryBuilder.add(query, BooleanClause.Occur.SHOULD);
314-
}
315-
return booleanQueryBuilder.build();
316-
}
317-
};
318-
}
319-
320299
public static BlendedTermQuery commonTermsBlendedQuery(Term[] terms, final float[] boosts, final float maxTermFrequency) {
321300
return new BlendedTermQuery(terms, boosts) {
322301
@Override

0 commit comments

Comments
 (0)