Search in sources :

Example 6 with SearchTemplateRequest

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());
}
Also used : RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) 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) 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) StringJoiner(java.util.StringJoiner)

Example 7 with SearchTemplateRequest

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)));
}
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 8 with SearchTemplateRequest

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());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 9 with SearchTemplateRequest

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;
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) 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 10 with SearchTemplateRequest

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());
}
Also used : HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) 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)

Aggregations

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