Search in sources :

Example 1 with MultiSearchTemplateRequest

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

the class RequestConverters method multiSearchTemplate.

static Request multiSearchTemplate(MultiSearchTemplateRequest multiSearchTemplateRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_msearch/template");
    Params params = new Params();
    params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true");
    if (multiSearchTemplateRequest.maxConcurrentSearchRequests() != MultiSearchRequest.MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT) {
        params.putParam("max_concurrent_searches", Integer.toString(multiSearchTemplateRequest.maxConcurrentSearchRequests()));
    }
    request.addParameters(params.asMap());
    XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
    byte[] source = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, xContent);
    request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
    return request;
}
Also used : NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) ToXContent(org.opensearch.common.xcontent.ToXContent) XContent(org.opensearch.common.xcontent.XContent) BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 2 with MultiSearchTemplateRequest

use of org.opensearch.script.mustache.MultiSearchTemplateRequest 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)));
}
Also used : RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) BytesArray(org.opensearch.common.bytes.BytesArray) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest)

Example 3 with MultiSearchTemplateRequest

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

the class SearchIT method testMultiSearchTemplateAllBad.

public void testMultiSearchTemplateAllBad() throws Exception {
    MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
    SearchTemplateRequest badRequest1 = new SearchTemplateRequest();
    badRequest1.setRequest(new SearchRequest("index"));
    badRequest1.setScriptType(ScriptType.INLINE);
    badRequest1.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }");
    Map<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("number", "BAD NUMBER");
    badRequest1.setScriptParams(scriptParams);
    multiSearchTemplateRequest.add(badRequest1);
    SearchTemplateRequest badRequest2 = new SearchTemplateRequest();
    badRequest2.setRequest(new SearchRequest("index"));
    badRequest2.setScriptType(ScriptType.INLINE);
    badRequest2.setScript("BAD QUERY TEMPLATE");
    scriptParams = new HashMap<>();
    scriptParams.put("number", "BAD NUMBER");
    badRequest2.setScriptParams(scriptParams);
    multiSearchTemplateRequest.add(badRequest2);
    // The whole HTTP request should fail if no nested search requests are valid
    OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, highLevelClient()::msearchTemplateAsync));
    assertEquals(RestStatus.BAD_REQUEST, exception.status());
    assertThat(exception.getMessage(), containsString("no requests added"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 4 with MultiSearchTemplateRequest

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

use of org.opensearch.script.mustache.MultiSearchTemplateRequest 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)

Aggregations

MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)6 SearchRequest (org.opensearch.action.search.SearchRequest)6 MultiSearchTemplateRequest (org.opensearch.script.mustache.MultiSearchTemplateRequest)6 SearchTemplateRequest (org.opensearch.script.mustache.SearchTemplateRequest)6 HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 MultiSearchTemplateResponse (org.opensearch.script.mustache.MultiSearchTemplateResponse)3 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)2 BulkRequest (org.opensearch.action.bulk.BulkRequest)2 DeleteRequest (org.opensearch.action.delete.DeleteRequest)2 ExplainRequest (org.opensearch.action.explain.ExplainRequest)2 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)2 GetRequest (org.opensearch.action.get.GetRequest)2 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)2 IndexRequest (org.opensearch.action.index.IndexRequest)2