Adds template support to _msearch resource
Much like we already do with search this adds templating support to the _msearch resource. Closes #10885
This commit is contained in:
parent
f0b7fa2f31
commit
8cd86a615a
|
@ -69,15 +69,15 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> implem
|
|||
}
|
||||
|
||||
public MultiSearchRequest add(byte[] data, int from, int length,
|
||||
@Nullable String[] indices, @Nullable String[] types, @Nullable String searchType) throws Exception {
|
||||
return add(new BytesArray(data, from, length), indices, types, searchType, null, IndicesOptions.strictExpandOpenAndForbidClosed(), true);
|
||||
boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType) throws Exception {
|
||||
return add(new BytesArray(data, from, length), isTemplateRequest, indices, types, searchType, null, IndicesOptions.strictExpandOpenAndForbidClosed(), true);
|
||||
}
|
||||
|
||||
public MultiSearchRequest add(BytesReference data, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IndicesOptions indicesOptions) throws Exception {
|
||||
return add(data, indices, types, searchType, null, indicesOptions, true);
|
||||
public MultiSearchRequest add(BytesReference data, boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, IndicesOptions indicesOptions) throws Exception {
|
||||
return add(data, isTemplateRequest, indices, types, searchType, null, indicesOptions, true);
|
||||
}
|
||||
|
||||
public MultiSearchRequest add(BytesReference data, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, @Nullable String routing, IndicesOptions indicesOptions, boolean allowExplicitIndex) throws Exception {
|
||||
public MultiSearchRequest add(BytesReference data, boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, @Nullable String routing, IndicesOptions indicesOptions, boolean allowExplicitIndex) throws Exception {
|
||||
XContent xContent = XContentFactory.xContent(data);
|
||||
int from = 0;
|
||||
int length = data.length();
|
||||
|
@ -146,8 +146,11 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> implem
|
|||
if (nextMarker == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
searchRequest.source(data.slice(from, nextMarker - from));
|
||||
if (isTemplateRequest) {
|
||||
searchRequest.templateSource(data.slice(from, nextMarker - from));
|
||||
} else {
|
||||
searchRequest.source(data.slice(from, nextMarker - from));
|
||||
}
|
||||
// move pointers
|
||||
from = nextMarker + 1;
|
||||
|
||||
|
@ -157,15 +160,6 @@ public class MultiSearchRequest extends ActionRequest<MultiSearchRequest> implem
|
|||
return this;
|
||||
}
|
||||
|
||||
private String[] parseArray(XContentParser parser) throws IOException {
|
||||
final List<String> list = new ArrayList<>();
|
||||
assert parser.currentToken() == XContentParser.Token.START_ARRAY;
|
||||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
|
||||
list.add(parser.text());
|
||||
}
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
private int findNextMarker(byte marker, int from, BytesReference data, int length) {
|
||||
for (int i = from; i < length; i++) {
|
||||
if (data.get(i) == marker) {
|
||||
|
|
|
@ -50,6 +50,13 @@ public class RestMultiSearchAction extends BaseRestHandler {
|
|||
controller.registerHandler(GET, "/{index}/{type}/_msearch", this);
|
||||
controller.registerHandler(POST, "/{index}/{type}/_msearch", this);
|
||||
|
||||
controller.registerHandler(GET, "/_msearch/template", this);
|
||||
controller.registerHandler(POST, "/_msearch/template", this);
|
||||
controller.registerHandler(GET, "/{index}/_msearch/template", this);
|
||||
controller.registerHandler(POST, "/{index}/_msearch/template", this);
|
||||
controller.registerHandler(GET, "/{index}/{type}/_msearch/template", this);
|
||||
controller.registerHandler(POST, "/{index}/{type}/_msearch/template", this);
|
||||
|
||||
this.allowExplicitIndex = settings.getAsBoolean("rest.action.multi.allow_explicit_index", true);
|
||||
}
|
||||
|
||||
|
@ -59,9 +66,15 @@ public class RestMultiSearchAction extends BaseRestHandler {
|
|||
|
||||
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
|
||||
String[] types = Strings.splitStringByCommaToArray(request.param("type"));
|
||||
String path = request.path();
|
||||
boolean isTemplateRequest = isTemplateRequest(path);
|
||||
IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
|
||||
multiSearchRequest.add(RestActions.getRestContent(request), indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);
|
||||
multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);
|
||||
|
||||
client.multiSearch(multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
|
||||
}
|
||||
|
||||
private boolean isTemplateRequest(String path) {
|
||||
return (path != null && path.endsWith("/template"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.action.search;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.test.StreamsUtils;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
|
@ -29,19 +28,16 @@ import org.elasticsearch.test.ESTestCase;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class MultiSearchRequestTests extends ESTestCase {
|
||||
|
||||
@Test
|
||||
public void simpleAdd() throws Exception {
|
||||
byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/search/simple-msearch1.json");
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, null, null, null);
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, false, null, null, null);
|
||||
assertThat(request.requests().size(), equalTo(8));
|
||||
assertThat(request.requests().get(0).indices()[0], equalTo("test"));
|
||||
assertThat(request.requests().get(0).indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed())));
|
||||
|
@ -67,7 +63,7 @@ public class MultiSearchRequestTests extends ESTestCase {
|
|||
@Test
|
||||
public void simpleAdd2() throws Exception {
|
||||
byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/search/simple-msearch2.json");
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, null, null, null);
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, false, null, null, null);
|
||||
assertThat(request.requests().size(), equalTo(5));
|
||||
assertThat(request.requests().get(0).indices()[0], equalTo("test"));
|
||||
assertThat(request.requests().get(0).types().length, equalTo(0));
|
||||
|
@ -81,11 +77,11 @@ public class MultiSearchRequestTests extends ESTestCase {
|
|||
assertThat(request.requests().get(4).indices(), nullValue());
|
||||
assertThat(request.requests().get(4).types().length, equalTo(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void simpleAdd3() throws Exception {
|
||||
byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/search/simple-msearch3.json");
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, null, null, null);
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, false, null, null, null);
|
||||
assertThat(request.requests().size(), equalTo(4));
|
||||
assertThat(request.requests().get(0).indices()[0], equalTo("test0"));
|
||||
assertThat(request.requests().get(0).indices()[1], equalTo("test1"));
|
||||
|
@ -104,7 +100,28 @@ public class MultiSearchRequestTests extends ESTestCase {
|
|||
@Test
|
||||
public void simpleAdd4() throws Exception {
|
||||
byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/search/simple-msearch4.json");
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, null, null, null);
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, false, null, null, null);
|
||||
assertThat(request.requests().size(), equalTo(3));
|
||||
assertThat(request.requests().get(0).indices()[0], equalTo("test0"));
|
||||
assertThat(request.requests().get(0).indices()[1], equalTo("test1"));
|
||||
assertThat(request.requests().get(0).requestCache(), equalTo(true));
|
||||
assertThat(request.requests().get(0).preference(), nullValue());
|
||||
assertThat(request.requests().get(1).indices()[0], equalTo("test2"));
|
||||
assertThat(request.requests().get(1).indices()[1], equalTo("test3"));
|
||||
assertThat(request.requests().get(1).types()[0], equalTo("type1"));
|
||||
assertThat(request.requests().get(1).requestCache(), nullValue());
|
||||
assertThat(request.requests().get(1).preference(), equalTo("_local"));
|
||||
assertThat(request.requests().get(2).indices()[0], equalTo("test4"));
|
||||
assertThat(request.requests().get(2).indices()[1], equalTo("test1"));
|
||||
assertThat(request.requests().get(2).types()[0], equalTo("type2"));
|
||||
assertThat(request.requests().get(2).types()[1], equalTo("type1"));
|
||||
assertThat(request.requests().get(2).routing(), equalTo("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleAdd5() throws Exception {
|
||||
byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/search/simple-msearch5.json");
|
||||
MultiSearchRequest request = new MultiSearchRequest().add(data, 0, data.length, true, null, null, null);
|
||||
assertThat(request.requests().size(), equalTo(3));
|
||||
assertThat(request.requests().get(0).indices()[0], equalTo("test0"));
|
||||
assertThat(request.requests().get(0).indices()[1], equalTo("test1"));
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
{"index":["test0", "test1"], "request_cache": true}
|
||||
{"template": {"query" : {"match_{{template}}" {}}}, "params": {"template": "all" } } }
|
||||
{"index" : "test2,test3", "type" : "type1", "preference": "_local"}
|
||||
{"template": {"query" : {"match_{{template}}" {}}}, "params": {"template": "all" } } }
|
||||
{"index" : ["test4", "test1"], "type" : [ "type2", "type1" ], "routing": "123"}
|
||||
{"template": {"query" : {"match_{{template}}" {}}}, "params": {"template": "all" } } }
|
|
@ -44,4 +44,10 @@
|
|||
- match: { responses.1.error.root_cause.0.index: test_2 }
|
||||
- match: { responses.2.hits.total: 1 }
|
||||
|
||||
|
||||
- do:
|
||||
msearch:
|
||||
body:
|
||||
- index: test_1
|
||||
- query:
|
||||
{ "template": { "query": { "term": { "foo": { "value": "{{template}}" } } }, "params": { "template": "bar" } } }
|
||||
- match: { responses.0.hits.total: 1 }
|
||||
|
|
Loading…
Reference in New Issue