This adds max_concurrent_searches to multi-search-template endpoint.

Closes #20912
This commit is contained in:
Isabel Drost-Fromm 2016-12-01 12:33:53 +01:00 committed by Martijn van Groningen
parent 51c74ce547
commit bd559d96d4
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
5 changed files with 48 additions and 0 deletions

View File

@ -34,6 +34,7 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
public class MultiSearchTemplateRequest extends ActionRequest implements CompositeIndicesRequest { public class MultiSearchTemplateRequest extends ActionRequest implements CompositeIndicesRequest {
private int maxConcurrentSearchRequests = 0;
private List<SearchTemplateRequest> requests = new ArrayList<>(); private List<SearchTemplateRequest> requests = new ArrayList<>();
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
@ -56,6 +57,26 @@ public class MultiSearchTemplateRequest extends ActionRequest implements Composi
return this; return this;
} }
/**
* Returns the amount of search requests specified in this multi search requests are allowed to be ran concurrently.
*/
public int maxConcurrentSearchRequests() {
return maxConcurrentSearchRequests;
}
/**
* Sets how many search requests specified in this multi search requests are allowed to be ran concurrently.
*/
public MultiSearchTemplateRequest maxConcurrentSearchRequests(int maxConcurrentSearchRequests) {
if (maxConcurrentSearchRequests < 1) {
throw new IllegalArgumentException("maxConcurrentSearchRequests must be positive");
}
this.maxConcurrentSearchRequests = maxConcurrentSearchRequests;
return this;
}
public List<SearchTemplateRequest> requests() { public List<SearchTemplateRequest> requests() {
return this.requests; return this.requests;
} }
@ -90,12 +111,14 @@ public class MultiSearchTemplateRequest extends ActionRequest implements Composi
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
maxConcurrentSearchRequests = in.readVInt();
requests = in.readStreamableList(SearchTemplateRequest::new); requests = in.readStreamableList(SearchTemplateRequest::new);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeVInt(maxConcurrentSearchRequests);
out.writeStreamableList(requests); out.writeStreamableList(requests);
} }
} }

View File

@ -58,4 +58,12 @@ public class MultiSearchTemplateRequestBuilder
request().indicesOptions(indicesOptions); request().indicesOptions(indicesOptions);
return this; return this;
} }
/**
* Sets how many search requests specified in this multi search requests are allowed to be ran concurrently.
*/
public MultiSearchTemplateRequestBuilder setMaxConcurrentSearchRequests(int maxConcurrentSearchRequests) {
request().maxConcurrentSearchRequests(maxConcurrentSearchRequests);
return this;
}
} }

View File

@ -70,6 +70,10 @@ public class RestMultiSearchTemplateAction extends BaseRestHandler {
*/ */
public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException { public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException {
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest(); MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
if (restRequest.hasParam("max_concurrent_searches")) {
multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0));
}
RestMultiSearchAction.parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex, RestMultiSearchAction.parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex,
(searchRequest, bytes) -> { (searchRequest, bytes) -> {
try { try {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.script.mustache; package org.elasticsearch.script.mustache;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
@ -90,4 +91,12 @@ public class MultiSearchTemplateRequestTests extends ESTestCase {
assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript()); assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript());
assertEquals(1, request.requests().get(0).getScriptParams().size()); assertEquals(1, request.requests().get(0).getScriptParams().size());
} }
public void testMaxConcurrentSearchRequests() {
MultiSearchTemplateRequest request = new MultiSearchTemplateRequest();
request.maxConcurrentSearchRequests(randomIntBetween(1, Integer.MAX_VALUE));
expectThrows(IllegalArgumentException.class, () ->
request.maxConcurrentSearchRequests(randomIntBetween(Integer.MIN_VALUE, 0)));
}
} }

View File

@ -24,6 +24,10 @@
"typed_keys": { "typed_keys": {
"type" : "boolean", "type" : "boolean",
"description" : "Specify whether aggregation and suggester names should be prefixed by their respective types in the response" "description" : "Specify whether aggregation and suggester names should be prefixed by their respective types in the response"
},
"max_concurrent_searches" : {
"type" : "number",
"description" : "Controls the maximum number of concurrent searches the multi search api will execute"
} }
} }
}, },