use of org.opensearch.script.mustache.SearchTemplateRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testSearchTemplate.
public void testSearchTemplate() throws Exception {
// Create a random request.
String[] indices = randomIndicesNames(0, 5);
SearchRequest searchRequest = new SearchRequest(indices);
Map<String, String> expectedParams = new HashMap<>();
setRandomSearchParams(searchRequest, expectedParams);
setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(searchRequest);
searchTemplateRequest.setScript("{\"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" }}}");
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setProfile(randomBoolean());
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "name");
scriptParams.put("value", "soren");
searchTemplateRequest.setScriptParams(scriptParams);
// Verify that the resulting REST request looks as expected.
Request request = RequestConverters.searchTemplate(searchTemplateRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_search/template");
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(searchTemplateRequest, request.getEntity());
}
use of org.opensearch.script.mustache.SearchTemplateRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testMultiSearchTemplate.
public void testMultiSearchTemplate() throws Exception {
final int numSearchRequests = randomIntBetween(1, 10);
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
for (int i = 0; i < numSearchRequests; i++) {
// Create a random request.
String[] indices = randomIndicesNames(0, 5);
SearchRequest searchRequest = new SearchRequest(indices);
Map<String, String> expectedParams = new HashMap<>();
setRandomSearchParams(searchRequest, expectedParams);
// scroll is not supported in the current msearch or msearchtemplate api, so unset it:
searchRequest.scroll((Scroll) null);
// batched reduce size is currently not set-able on a per-request basis as it is a query string parameter only
searchRequest.setBatchedReduceSize(SearchRequest.DEFAULT_BATCHED_REDUCE_SIZE);
setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(searchRequest);
searchTemplateRequest.setScript("{\"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" }}}");
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setProfile(randomBoolean());
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "name");
scriptParams.put("value", randomAlphaOfLengthBetween(2, 5));
searchTemplateRequest.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(searchTemplateRequest);
}
Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
multiSearchTemplateRequest.maxConcurrentSearchRequests(randomIntBetween(1, 10));
expectedParams.put("max_concurrent_searches", Integer.toString(multiSearchTemplateRequest.maxConcurrentSearchRequests()));
}
expectedParams.put(RestSearchAction.TYPED_KEYS_PARAM, "true");
Request multiRequest = RequestConverters.multiSearchTemplate(multiSearchTemplateRequest);
assertEquals(HttpPost.METHOD_NAME, multiRequest.getMethod());
assertEquals("/_msearch/template", multiRequest.getEndpoint());
List<SearchTemplateRequest> searchRequests = multiSearchTemplateRequest.requests();
assertEquals(numSearchRequests, searchRequests.size());
assertEquals(expectedParams, multiRequest.getParameters());
HttpEntity actualEntity = multiRequest.getEntity();
byte[] expectedBytes = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, XContentType.JSON.xContent());
assertEquals(XContentType.JSON.mediaTypeWithoutParameters(), actualEntity.getContentType().getValue());
assertEquals(new BytesArray(expectedBytes), new BytesArray(EntityUtils.toByteArray(actualEntity)));
}
use of org.opensearch.script.mustache.SearchTemplateRequest in project OpenSearch by opensearch-project.
the class SearchIT method testNonExistentSearchTemplate.
public void testNonExistentSearchTemplate() {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest("index"));
searchTemplateRequest.setScriptType(ScriptType.STORED);
searchTemplateRequest.setScript("non-existent");
searchTemplateRequest.setScriptParams(Collections.emptyMap());
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(searchTemplateRequest, highLevelClient()::searchTemplate, highLevelClient()::searchTemplateAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}
use of org.opensearch.script.mustache.SearchTemplateRequest in project OpenSearch by opensearch-project.
the class RequestConverters method searchTemplate.
static Request searchTemplate(SearchTemplateRequest searchTemplateRequest) throws IOException {
Request request;
if (searchTemplateRequest.isSimulate()) {
request = new Request(HttpGet.METHOD_NAME, "_render/template");
} else {
SearchRequest searchRequest = searchTemplateRequest.getRequest();
String endpoint = endpoint(searchRequest.indices(), "_search/template");
request = new Request(HttpGet.METHOD_NAME, endpoint);
Params params = new Params();
addSearchRequestParams(params, searchRequest);
request.addParameters(params.asMap());
}
request.setEntity(createEntity(searchTemplateRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
use of org.opensearch.script.mustache.SearchTemplateRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testRenderSearchTemplate.
public void testRenderSearchTemplate() throws Exception {
// Create a simple request.
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
// Setting simulate true means the template should only be rendered.
searchTemplateRequest.setSimulate(true);
searchTemplateRequest.setScript("template1");
searchTemplateRequest.setScriptType(ScriptType.STORED);
searchTemplateRequest.setProfile(randomBoolean());
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "name");
scriptParams.put("value", "soren");
searchTemplateRequest.setScriptParams(scriptParams);
// Verify that the resulting REST request looks as expected.
Request request = RequestConverters.searchTemplate(searchTemplateRequest);
String endpoint = "_render/template";
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint, request.getEndpoint());
assertEquals(Collections.emptyMap(), request.getParameters());
assertToXContentBody(searchTemplateRequest, request.getEntity());
}
Aggregations