-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add async_search.submit API to HLRC (#53592)
This commit adds a new AsyncSearchClient to the High Level Rest Client which initially supporst the submitAsyncSearch in its blocking and non-blocking flavour. Also adding client side request and response objects and parsing code to parse the xContent output of the client side AsyncSearchResponse together with parsing roundtrip tests and a simple roundtrip integration test. Relates to #49091
- Loading branch information
Christoph Büscher
authored
Mar 19, 2020
1 parent
2e617c3
commit dea6b7d
Showing
14 changed files
with
1,020 additions
and
23 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.client.asyncsearch.AsyncSearchResponse; | ||
import org.elasticsearch.client.asyncsearch.SubmitAsyncSearchRequest; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
public class AsyncSearchClient { | ||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
AsyncSearchClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Submit a new async search request. | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/async-search.html"> the docs</a> for more. | ||
* @param request the request | ||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @return the response | ||
* @throws IOException in case there is a problem sending the request or parsing back the response | ||
*/ | ||
public AsyncSearchResponse submitAsyncSearch(SubmitAsyncSearchRequest request, RequestOptions options) throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(request, AsyncSearchRequestConverters::submitAsyncSearch, options, | ||
AsyncSearchResponse::fromXContent, emptySet()); | ||
} | ||
|
||
/** | ||
* Asynchronously submit a new async search request. | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/async-search.html"> the docs</a> for more. | ||
* <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-ilm-ilm-get-lifecycle-policy.html"> | ||
* the docs</a> for more. | ||
* @param request the request | ||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized | ||
* @param listener the listener to be notified upon request completion | ||
* @return cancellable that may be used to cancel the request | ||
*/ | ||
public Cancellable submitAsyncSearchAsync(SubmitAsyncSearchRequest request, RequestOptions options, | ||
ActionListener<AsyncSearchResponse> listener) { | ||
return restHighLevelClient.performRequestAsyncAndParseEntity(request, AsyncSearchRequestConverters::submitAsyncSearch, options, | ||
AsyncSearchResponse::fromXContent, listener, emptySet()); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
.../rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchRequestConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.apache.http.client.methods.HttpPost; | ||
import org.elasticsearch.client.RequestConverters.Params; | ||
import org.elasticsearch.client.asyncsearch.SubmitAsyncSearchRequest; | ||
import org.elasticsearch.rest.action.search.RestSearchAction; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE; | ||
|
||
final class AsyncSearchRequestConverters { | ||
|
||
static Request submitAsyncSearch(SubmitAsyncSearchRequest asyncSearchRequest) throws IOException { | ||
String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts( | ||
asyncSearchRequest.getIndices()) | ||
.addPathPartAsIs("_async_search").build(); | ||
Request request = new Request(HttpPost.METHOD_NAME, endpoint); | ||
Params params = new RequestConverters.Params(); | ||
// add all typical search params and search request source as body | ||
addSearchRequestParams(params, asyncSearchRequest); | ||
if (asyncSearchRequest.getSearchSource() != null) { | ||
request.setEntity(RequestConverters.createEntity(asyncSearchRequest.getSearchSource(), REQUEST_BODY_CONTENT_TYPE)); | ||
} | ||
// set async search submit specific parameters | ||
if (asyncSearchRequest.isCleanOnCompletion() != null) { | ||
params.putParam("clean_on_completion", asyncSearchRequest.isCleanOnCompletion().toString()); | ||
} | ||
if (asyncSearchRequest.getKeepAlive() != null) { | ||
params.putParam("keep_alive", asyncSearchRequest.getKeepAlive().getStringRep()); | ||
} | ||
if (asyncSearchRequest.getWaitForCompletion() != null) { | ||
params.putParam("wait_for_completion", asyncSearchRequest.getWaitForCompletion().getStringRep()); | ||
} | ||
request.addParameters(params.asMap()); | ||
return request; | ||
} | ||
|
||
static void addSearchRequestParams(Params params, SubmitAsyncSearchRequest request) { | ||
params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true"); | ||
params.withRouting(request.getRouting()); | ||
params.withPreference(request.getPreference()); | ||
params.withIndicesOptions(request.getIndicesOptions()); | ||
params.withSearchType(request.getSearchType().name().toLowerCase(Locale.ROOT)); | ||
params.withMaxConcurrentShardRequests(request.getMaxConcurrentShardRequests()); | ||
if (request.getRequestCache() != null) { | ||
params.withRequestCache(request.getRequestCache()); | ||
} | ||
if (request.getAllowPartialSearchResults() != null) { | ||
params.withAllowPartialResults(request.getAllowPartialSearchResults()); | ||
} | ||
params.withBatchedReduceSize(request.getBatchedReduceSize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.