Search in sources :

Example 1 with SearchTemplateResponse

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());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with SearchTemplateResponse

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);
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Item(org.opensearch.script.mustache.MultiSearchTemplateResponse.Item) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse)

Example 3 with SearchTemplateResponse

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));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse)

Example 4 with SearchTemplateResponse

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());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with SearchTemplateResponse

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);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 MultiSearchTemplateRequest (org.opensearch.script.mustache.MultiSearchTemplateRequest)5 MultiSearchTemplateResponse (org.opensearch.script.mustache.MultiSearchTemplateResponse)5 SearchTemplateRequest (org.opensearch.script.mustache.SearchTemplateRequest)5 SearchTemplateResponse (org.opensearch.script.mustache.SearchTemplateResponse)5 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)4 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)4 SearchRequest (org.opensearch.action.search.SearchRequest)4 SearchResponse (org.opensearch.action.search.SearchResponse)4 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)3 BytesReference (org.opensearch.common.bytes.BytesReference)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 RestClient (org.opensearch.client.RestClient)1 Item (org.opensearch.script.mustache.MultiSearchTemplateResponse.Item)1 SearchHit (org.opensearch.search.SearchHit)1