Skip to content

Commit

Permalink
Avoid additional boilerplate getters on SubmitAsyncSearchRequest by m…
Browse files Browse the repository at this point in the history
…aking getSearchRequest() package private
  • Loading branch information
Christoph Büscher committed Mar 18, 2020
1 parent 594a5eb commit f686a50
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.elasticsearch.client;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.asyncsearch.SubmitAsyncSearchRequest;
import org.elasticsearch.client.asyncsearch.AsyncSearchResponse;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpPost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RequestConverters.Params;
import org.elasticsearch.client.asyncsearch.SubmitAsyncSearchRequest;
import org.elasticsearch.rest.action.search.RestSearchAction;

import java.io.IOException;
Expand All @@ -37,10 +37,11 @@ static Request submitAsyncSearch(SubmitAsyncSearchRequest asyncSearchRequest) th
.addPathPartAsIs("_async_search").build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
Params params = new RequestConverters.Params();
SearchRequest searchRequest = asyncSearchRequest.getSearchRequest();
// 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));
addSearchRequestParams(params, searchRequest);
if (searchRequest.source() != null) {
request.setEntity(RequestConverters.createEntity(searchRequest.source(), REQUEST_BODY_CONTENT_TYPE));
}
// set async search submit specific parameters
if (asyncSearchRequest.isCleanOnCompletion() != null) {
Expand All @@ -56,17 +57,17 @@ static Request submitAsyncSearch(SubmitAsyncSearchRequest asyncSearchRequest) th
return request;
}

static void addSearchRequestParams(Params params, SubmitAsyncSearchRequest request) {
static void addSearchRequestParams(Params params, SearchRequest request) {
params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true");
params.withRouting(request.getRouting());
params.withPreference(request.getPreference());
params.withIndicesOptions(request.getIndicesOptions());
params.putParam("search_type", request.getSearchType().name().toLowerCase(Locale.ROOT));
if (request.getRequestCache() != null) {
params.putParam("request_cache", Boolean.toString(request.getRequestCache()));
params.withRouting(request.routing());
params.withPreference(request.preference());
params.withIndicesOptions(request.indicesOptions());
params.putParam("search_type", request.searchType().name().toLowerCase(Locale.ROOT));
if (request.requestCache() != null) {
params.putParam("request_cache", Boolean.toString(request.requestCache()));
}
if (request.getAllowPartialSearchResults() != null) {
params.putParam("allow_partial_search_results", Boolean.toString(request.getAllowPartialSearchResults()));
if (request.allowPartialSearchResults() != null) {
params.putParam("allow_partial_search_results", Boolean.toString(request.allowPartialSearchResults()));
}
params.putParam("batched_reduce_size", Integer.toString(request.getBatchedReduceSize()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.asyncsearch;
package org.elasticsearch.client;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
*/


package org.elasticsearch.client.asyncsearch;
package org.elasticsearch.client;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Validatable;
import org.elasticsearch.client.ValidationException;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.builder.SearchSourceBuilder;

Expand Down Expand Up @@ -60,12 +58,12 @@ public SubmitAsyncSearchRequest(SearchSourceBuilder source, String... indices) {
}


// /**
// * Get the {@link SearchRequest} that this submit request wraps
// */
// public SearchRequest getSearchRequest() {
// return searchRequest;
// }
/**
* Get the {@link SearchRequest} that this submit request wraps
*/
SearchRequest getSearchRequest() {
return searchRequest;
}

/**
* Get the target indices
Expand Down Expand Up @@ -133,13 +131,6 @@ public void setRoutings(String... routings) {
this.searchRequest.routing(routings);
}

/**
* Get the routing value to control the shards that the search will be executed on.
*/
public String getRouting() {
return this.searchRequest.routing();
}

/**
* Sets the preference to execute the search. Defaults to randomize across shards. Can be set to
* {@code _local} to prefer local shards or a custom value, which guarantees that the same order
Expand All @@ -149,41 +140,20 @@ public void setPreference(String preference) {
this.searchRequest.preference(preference);
}

/**
* Get the preference to execute the search.
*/
public String getPreference() {
return this.searchRequest.preference();
}

/**
* Specifies what type of requested indices to ignore and how to deal with indices wildcard expressions.
*/
public void setIndicesOptions(IndicesOptions indicesOptions) {
this.searchRequest.indicesOptions(indicesOptions);
}

/**
* Get the indices Options.
*/
public IndicesOptions getIndicesOptions() {
return this.searchRequest.indicesOptions();
}

/**
* The search type to execute, defaults to {@link SearchType#DEFAULT}.
*/
public void setSearchType(SearchType searchType) {
this.searchRequest.searchType(searchType);
}

/**
* Get the search type to execute, defaults to {@link SearchType#DEFAULT}.
*/
public SearchType getSearchType() {
return this.searchRequest.searchType();
}

/**
* Sets if this request should allow partial results. (If method is not called,
* will default to the cluster level setting).
Expand All @@ -192,13 +162,6 @@ public void setAllowPartialSearchResults(boolean allowPartialSearchResults) {
this.searchRequest.allowPartialSearchResults(allowPartialSearchResults);
}

/**
* Gets if this request should allow partial results.
*/
public Boolean getAllowPartialSearchResults() {
return this.searchRequest.allowPartialSearchResults();
}

/**
* Sets the number of shard results that should be reduced at once on the coordinating node. This value should be used as a protection
* mechanism to reduce the memory overhead per search request if the potential number of shards in the request can be large.
Expand All @@ -207,14 +170,6 @@ public void setBatchedReduceSize(int batchedReduceSize) {
this.searchRequest.setBatchedReduceSize(batchedReduceSize);
}

/**
* Gets the number of shard results that should be reduced at once on the coordinating node.
* This defaults to 5 for {@link SubmitAsyncSearchRequest}.
*/
public int getBatchedReduceSize() {
return this.searchRequest.getBatchedReduceSize();
}

/**
* Sets if this request should use the request cache or not, assuming that it can (for
* example, if "now" is used, it will never be cached). By default (not set, or null,
Expand All @@ -224,21 +179,6 @@ public void setRequestCache(Boolean requestCache) {
this.searchRequest.requestCache(requestCache);
}

/**
* Gets if this request should use the request cache or not.
* Defaults to `true` for {@link SubmitAsyncSearchRequest}.
*/
public Boolean getRequestCache() {
return this.searchRequest.requestCache();
}

/**
* Gets if the source of the {@link SearchSourceBuilder} initially used on this request.
*/
public SearchSourceBuilder getSearchSource() {
return this.searchRequest.source();
}

@Override
public Optional<ValidationException> validate() {
final ValidationException validationException = new ValidationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpPost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.asyncsearch.SubmitAsyncSearchRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.rest.action.search.RestSearchAction;
Expand All @@ -48,8 +48,9 @@ public void testSubmitAsyncSearch() throws Exception {
// but we need to set these since they are the default we send over http
expectedParams.put("request_cache", "true");
expectedParams.put("batched_reduce_size", "5");
setRandomSearchParams(submitRequest, expectedParams);
setRandomIndicesOptions(submitRequest::setIndicesOptions, submitRequest::getIndicesOptions, expectedParams);
SearchRequest searchRequest = submitRequest.getSearchRequest();
setRandomSearchParams(searchRequest, expectedParams);
setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);

if (randomBoolean()) {
boolean cleanOnCompletion = randomBoolean();
Expand Down Expand Up @@ -80,27 +81,27 @@ public void testSubmitAsyncSearch() throws Exception {
RequestConvertersTests.assertToXContentBody(searchSourceBuilder, request.getEntity());
}

private static void setRandomSearchParams(SubmitAsyncSearchRequest request, Map<String, String> expectedParams) {
private static void setRandomSearchParams(SearchRequest request, Map<String, String> expectedParams) {
expectedParams.put(RestSearchAction.TYPED_KEYS_PARAM, "true");
if (randomBoolean()) {
request.setRouting(randomAlphaOfLengthBetween(3, 10));
expectedParams.put("routing", request.getRouting());
request.routing(randomAlphaOfLengthBetween(3, 10));
expectedParams.put("routing", request.routing());
}
if (randomBoolean()) {
request.setPreference(randomAlphaOfLengthBetween(3, 10));
expectedParams.put("preference", request.getPreference());
request.preference(randomAlphaOfLengthBetween(3, 10));
expectedParams.put("preference", request.preference());
}
if (randomBoolean()) {
request.setSearchType(randomFrom(SearchType.CURRENTLY_SUPPORTED));
request.searchType(randomFrom(SearchType.CURRENTLY_SUPPORTED));
}
expectedParams.put("search_type", request.getSearchType().name().toLowerCase(Locale.ROOT));
expectedParams.put("search_type", request.searchType().name().toLowerCase(Locale.ROOT));
if (randomBoolean()) {
request.setAllowPartialSearchResults(randomBoolean());
expectedParams.put("allow_partial_search_results", Boolean.toString(request.getAllowPartialSearchResults()));
request.allowPartialSearchResults(randomBoolean());
expectedParams.put("allow_partial_search_results", Boolean.toString(request.allowPartialSearchResults()));
}
if (randomBoolean()) {
request.setRequestCache(randomBoolean());
expectedParams.put("request_cache", Boolean.toString(request.getRequestCache()));
request.requestCache(randomBoolean());
expectedParams.put("request_cache", Boolean.toString(request.requestCache()));
}
if (randomBoolean()) {
request.setBatchedReduceSize(randomIntBetween(2, Integer.MAX_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.asyncsearch;
package org.elasticsearch.client;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchResponse.Clusters;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.client.AbstractResponseTestCase;
import org.elasticsearch.client.AsyncSearchResponse;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.internal.InternalSearchResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

package org.elasticsearch.client.asyncsearch;
package org.elasticsearch.client;

import org.elasticsearch.client.SubmitAsyncSearchRequest;
import org.elasticsearch.client.ValidationException;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.builder.SearchSourceBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

package org.elasticsearch.client.asyncsearch;

import org.elasticsearch.client.AsyncSearchResponse;
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.SubmitAsyncSearchRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
Expand Down

0 comments on commit f686a50

Please sign in to comment.