use of org.opensearch.script.mustache.SearchTemplateResponse in project OpenSearch by opensearch-project.
the class SearchIT method testSearchTemplate.
public void testSearchTemplate() throws IOException {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setRequest(new SearchRequest("index"));
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("number", 10);
searchTemplateRequest.setScriptParams(scriptParams);
searchTemplateRequest.setExplain(true);
searchTemplateRequest.setProfile(true);
SearchTemplateResponse searchTemplateResponse = execute(searchTemplateRequest, highLevelClient()::searchTemplate, highLevelClient()::searchTemplateAsync);
assertNull(searchTemplateResponse.getSource());
SearchResponse searchResponse = searchTemplateResponse.getResponse();
assertNotNull(searchResponse);
assertEquals(1, searchResponse.getHits().getTotalHits().value);
assertEquals(1, searchResponse.getHits().getHits().length);
assertThat(searchResponse.getHits().getMaxScore(), greaterThan(0f));
SearchHit hit = searchResponse.getHits().getHits()[0];
assertNotNull(hit.getExplanation());
assertFalse(searchResponse.getProfileResults().isEmpty());
}
use of org.opensearch.script.mustache.SearchTemplateResponse in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testMultiSearchTemplateWithInlineScript.
@SuppressWarnings("unused")
public void testMultiSearchTemplateWithInlineScript() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
// tag::multi-search-template-request-inline
String[] searchTerms = { "opensearch", "opensearch-dashboards" };
// <1>
MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
for (String searchTerm : searchTerms) {
// <2>
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.INLINE);
request.setScript("{" + " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + "}");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "title");
scriptParams.put("value", searchTerm);
scriptParams.put("size", 5);
request.setScriptParams(scriptParams);
// <3>
multiRequest.add(request);
}
// end::multi-search-template-request-inline
// tag::multi-search-template-request-sync
MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
// tag::multi-search-template-response
for (Item item : multiResponse.getResponses()) {
// <1>
if (item.isFailure()) {
// <2>
String error = item.getFailureMessage();
} else {
// <3>
SearchTemplateResponse searchTemplateResponse = item.getResponse();
SearchResponse searchResponse = searchTemplateResponse.getResponse();
searchResponse.getHits();
}
}
// end::multi-search-template-response
assertNotNull(multiResponse);
assertEquals(searchTerms.length, multiResponse.getResponses().length);
assertNotNull(multiResponse.getResponses()[0]);
SearchResponse searchResponse = multiResponse.getResponses()[0].getResponse().getResponse();
assertTrue(searchResponse.getHits().getTotalHits().value > 0);
}
use of org.opensearch.script.mustache.SearchTemplateResponse in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testSearchTemplateWithStoredScript.
public void testSearchTemplateWithStoredScript() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
RestClient restClient = client();
registerQueryScript(restClient);
// tag::search-template-request-stored
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.STORED);
request.setScript("title_search");
Map<String, Object> params = new HashMap<>();
params.put("field", "title");
params.put("value", "opensearch");
params.put("size", 5);
request.setScriptParams(params);
// end::search-template-request-stored
// tag::search-template-request-options
request.setExplain(true);
request.setProfile(true);
// end::search-template-request-options
// tag::search-template-execute
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
// end::search-template-execute
SearchResponse searchResponse = response.getResponse();
assertNotNull(searchResponse);
assertTrue(searchResponse.getHits().getTotalHits().value > 0);
// tag::search-template-execute-listener
ActionListener<SearchTemplateResponse> listener = new ActionListener<SearchTemplateResponse>() {
@Override
public void onResponse(SearchTemplateResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::search-template-execute-listener
// Replace the empty listener by a blocking listener for tests.
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::search-template-execute-async
// <1>
client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
// end::search-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.script.mustache.SearchTemplateResponse in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testSearchTemplateWithInlineScript.
public void testSearchTemplateWithInlineScript() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
// tag::search-template-request-inline
SearchTemplateRequest request = new SearchTemplateRequest();
// <1>
request.setRequest(new SearchRequest("posts"));
request.setScriptType(ScriptType.INLINE);
// <2>
request.setScript("{" + " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + "}");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("field", "title");
scriptParams.put("value", "opensearch");
scriptParams.put("size", 5);
// <3>
request.setScriptParams(scriptParams);
// end::search-template-request-inline
// tag::search-template-response
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
// end::search-template-response
assertNotNull(searchResponse);
assertTrue(searchResponse.getHits().getTotalHits().value > 0);
// tag::render-search-template-request
// <1>
request.setSimulate(true);
// end::render-search-template-request
// tag::render-search-template-response
SearchTemplateResponse renderResponse = client.searchTemplate(request, RequestOptions.DEFAULT);
// <1>
BytesReference source = renderResponse.getSource();
// end::render-search-template-response
assertNotNull(source);
assertEquals(("{ \"size\" : \"5\", \"query\": { \"match\" : { \"title\" : \"opensearch\" } }}").replaceAll("\\s+", ""), source.utf8ToString());
}
use of org.opensearch.script.mustache.SearchTemplateResponse in project OpenSearch by opensearch-project.
the class SearchIT method testRenderSearchTemplate.
public void testRenderSearchTemplate() throws IOException {
SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
searchTemplateRequest.setScriptType(ScriptType.INLINE);
searchTemplateRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("number", 10);
searchTemplateRequest.setScriptParams(scriptParams);
// Setting simulate true causes the template to only be rendered.
searchTemplateRequest.setSimulate(true);
SearchTemplateResponse searchTemplateResponse = execute(searchTemplateRequest, highLevelClient()::searchTemplate, highLevelClient()::searchTemplateAsync);
assertNull(searchTemplateResponse.getResponse());
BytesReference expectedSource = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("query").startObject("match").field("num", 10).endObject().endObject().endObject());
BytesReference actualSource = searchTemplateResponse.getSource();
assertNotNull(actualSource);
assertToXContentEquivalent(expectedSource, actualSource, XContentType.JSON);
}
Aggregations