Search in sources :

Example 1 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 2 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 3 with SearchTemplateRequest

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

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

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