Search in sources :

Example 1 with MultiSearchTemplateResponse

use of org.opensearch.script.mustache.MultiSearchTemplateResponse 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 2 with MultiSearchTemplateResponse

use of org.opensearch.script.mustache.MultiSearchTemplateResponse in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testMultiSearchTemplateWithStoredScript.

public void testMultiSearchTemplateWithStoredScript() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    RestClient restClient = client();
    registerQueryScript(restClient);
    // tag::multi-search-template-request-stored
    MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
    String[] searchTerms = { "opensearch", "dashboards" };
    for (String searchTerm : searchTerms) {
        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", searchTerm);
        params.put("size", 5);
        request.setScriptParams(params);
        multiRequest.add(request);
    }
    // end::multi-search-template-request-stored
    // tag::multi-search-template-execute
    MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
    // end::multi-search-template-execute
    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);
    // tag::multi-search-template-execute-listener
    ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {

        @Override
        public void onResponse(MultiSearchTemplateResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::multi-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::multi-search-template-execute-async
    client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
    // end::multi-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) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) 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) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest)

Example 3 with MultiSearchTemplateResponse

use of org.opensearch.script.mustache.MultiSearchTemplateResponse in project OpenSearch by opensearch-project.

the class SearchIT method testMultiSearchTemplate.

public void testMultiSearchTemplate() throws Exception {
    MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
    SearchTemplateRequest goodRequest = new SearchTemplateRequest();
    goodRequest.setRequest(new SearchRequest("index"));
    goodRequest.setScriptType(ScriptType.INLINE);
    goodRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }");
    Map<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("number", 10);
    goodRequest.setScriptParams(scriptParams);
    goodRequest.setExplain(true);
    goodRequest.setProfile(true);
    multiSearchTemplateRequest.add(goodRequest);
    SearchTemplateRequest badRequest = new SearchTemplateRequest();
    badRequest.setRequest(new SearchRequest("index"));
    badRequest.setScriptType(ScriptType.INLINE);
    badRequest.setScript("{ NOT VALID JSON {{number}} }");
    scriptParams = new HashMap<>();
    scriptParams.put("number", 10);
    badRequest.setScriptParams(scriptParams);
    multiSearchTemplateRequest.add(badRequest);
    MultiSearchTemplateResponse multiSearchTemplateResponse = execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, highLevelClient()::msearchTemplateAsync);
    Item[] responses = multiSearchTemplateResponse.getResponses();
    assertEquals(2, responses.length);
    assertNull(responses[0].getResponse().getSource());
    SearchResponse goodResponse = responses[0].getResponse().getResponse();
    assertNotNull(goodResponse);
    assertThat(responses[0].isFailure(), Matchers.is(false));
    assertEquals(1, goodResponse.getHits().getTotalHits().value);
    assertEquals(1, goodResponse.getHits().getHits().length);
    assertThat(goodResponse.getHits().getMaxScore(), greaterThan(0f));
    SearchHit hit = goodResponse.getHits().getHits()[0];
    assertNotNull(hit.getExplanation());
    assertFalse(goodResponse.getProfileResults().isEmpty());
    assertNull(responses[0].getResponse().getSource());
    assertThat(responses[1].isFailure(), Matchers.is(true));
    assertNotNull(responses[1].getFailureMessage());
    assertThat(responses[1].getFailureMessage(), containsString("json_parse_exception"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) HashMap(java.util.HashMap) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) 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)

Aggregations

HashMap (java.util.HashMap)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)3 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 MultiSearchTemplateRequest (org.opensearch.script.mustache.MultiSearchTemplateRequest)3 MultiSearchTemplateResponse (org.opensearch.script.mustache.MultiSearchTemplateResponse)3 SearchTemplateRequest (org.opensearch.script.mustache.SearchTemplateRequest)3 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)2 Item (org.opensearch.script.mustache.MultiSearchTemplateResponse.Item)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 SearchTemplateResponse (org.opensearch.script.mustache.SearchTemplateResponse)1 SearchHit (org.opensearch.search.SearchHit)1