org.elasticsearch.action.search.template.SearchTemplateResponse

Here are the examples of the java api class org.elasticsearch.action.search.template.SearchTemplateResponse taken from open source projects.

1. TemplateQueryTests#testSearchRequestFail()

Project: elasticsearch
File: TemplateQueryTests.java
// Relates to #6318
public void testSearchRequestFail() throws Exception {
    String query = "{ \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\"  }";
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    expectThrows(Exception.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(searchRequest).setScript(query).setScriptType(ScriptType.INLINE).setScriptParams(randomBoolean() ? null : Collections.emptyMap()).get());
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(searchRequest).setScript(query).setScriptType(ScriptType.INLINE).setScriptParams(Collections.singletonMap("my_size", 1)).get();
    assertThat(searchResponse.getResponse().getHits().hits().length, equalTo(1));
}

2. TemplateQueryTests#testIndexedTemplateWithArray()

Project: elasticsearch
File: TemplateQueryTests.java
public void testIndexedTemplateWithArray() throws Exception {
    String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("4").setSource(jsonBuilder().startObject().field("template", multiQuery).endObject().bytes()));
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
    builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
    builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
    builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
    builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
    indexRandom(true, builders);
    Map<String, Object> arrayTemplateParams = new HashMap<>();
    String[] fieldParams = { "foo", "bar" };
    arrayTemplateParams.put("fieldParam", fieldParams);
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("/mustache/4").setScriptType(ScriptService.ScriptType.STORED).setScriptParams(arrayTemplateParams).get();
    assertHitCount(searchResponse.getResponse(), 5);
}

3. TemplateQueryTests#testIndexedTemplate()

Project: elasticsearch
File: TemplateQueryTests.java
public void testIndexedTemplate() throws Exception {
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("1a").setSource(new BytesArray("{" + "\"template\":{" + "                \"query\":{" + "                   \"match\":{" + "                    \"theField\" : \"{{fieldParam}}\"}" + "       }" + "}" + "}")));
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("2").setSource(new BytesArray("{" + "\"template\":{" + "                \"query\":{" + "                   \"match\":{" + "                    \"theField\" : \"{{fieldParam}}\"}" + "       }" + "}" + "}")));
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("3").setSource(new BytesArray("{" + "\"template\":{" + "             \"match\":{" + "                    \"theField\" : \"{{fieldParam}}\"}" + "       }" + "}")));
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
    builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
    builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
    builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
    builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
    indexRandom(true, builders);
    Map<String, Object> templateParams = new HashMap<>();
    templateParams.put("fieldParam", "foo");
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("/mustache/1a").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 4);
    expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("/template_index/mustache/1000").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
    expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("/myindex/mustache/1").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
    templateParams.put("fieldParam", "bar");
    searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("/mustache/2").setScriptType(ScriptService.ScriptType.STORED).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 1);
    Map<String, Object> vars = new HashMap<>();
    vars.put("fieldParam", "bar");
    TemplateQueryBuilder builder = new TemplateQueryBuilder(new Template("3", ScriptService.ScriptType.STORED, null, null, vars));
    SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
    assertHitCount(sr, 1);
    // "{\"template\": {\"id\": \"3\",\"params\" : {\"fieldParam\" : \"foo\"}}}";
    Map<String, Object> params = new HashMap<>();
    params.put("fieldParam", "foo");
    TemplateQueryBuilder templateQuery = new TemplateQueryBuilder(new Template("3", ScriptType.STORED, null, null, params));
    sr = client().prepareSearch().setQuery(templateQuery).get();
    assertHitCount(sr, 4);
    templateQuery = new TemplateQueryBuilder(new Template("/mustache/3", ScriptType.STORED, null, null, params));
    sr = client().prepareSearch().setQuery(templateQuery).get();
    assertHitCount(sr, 4);
}

4. TemplateQueryTests#testIndexedTemplateClient()

Project: elasticsearch
File: TemplateQueryTests.java
public void testIndexedTemplateClient() throws Exception {
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("testTemplate").setSource(new BytesArray("{" + "\"template\":{" + "                \"query\":{" + "                   \"match\":{" + "                    \"theField\" : \"{{fieldParam}}\"}" + "       }" + "}" + "}")));
    assertAcked(client().admin().cluster().preparePutStoredScript().setScriptLang(MustacheScriptEngineService.NAME).setId("testTemplate").setSource(new BytesArray("{" + "\"template\":{" + "                \"query\":{" + "                   \"match\":{" + "                    \"theField\" : \"{{fieldParam}}\"}" + "       }" + "}" + "}")));
    GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "testTemplate").get();
    assertNotNull(getResponse.getStoredScript());
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
    builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
    builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
    builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
    builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));
    indexRandom(true, builders);
    Map<String, Object> templateParams = new HashMap<>();
    templateParams.put("fieldParam", "foo");
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("testTemplate").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 4);
    assertAcked(client().admin().cluster().prepareDeleteStoredScript(MustacheScriptEngineService.NAME, "testTemplate"));
    getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "testTemplate").get();
    assertNull(getResponse.getStoredScript());
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("/template_index/mustache/1000").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
    assertThat(e.getMessage(), containsString("Illegal index script format [/template_index/mustache/1000] should be /lang/id"));
}

5. TemplateQueryTests#testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd()

Project: elasticsearch
File: TemplateQueryTests.java
/**
     * Test that template can contain conditional clause. In this case it is at
     * the end of the string.
     */
public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String templateString = "{" + "  \"inline\" : \"{ \\\"query\\\":{\\\"match_all\\\":{}} {{#use_size}}, \\\"size\\\": \\\"{{size}}\\\" {{/use_size}} }\"," + "  \"params\":{" + "    \"size\": 1," + "    \"use_size\": true" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(new BytesArray(templateString));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().hits().length, equalTo(1));
}

6. TemplateQueryTests#testTemplateQueryAsEscapedStringStartingWithConditionalClause()

Project: elasticsearch
File: TemplateQueryTests.java
/**
     * Test that template can contain conditional clause. In this case it is at
     * the beginning of the string.
     */
public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String templateString = "{" + "  \"inline\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\"," + "  \"params\":{" + "    \"size\": 1," + "    \"use_size\": true" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(new BytesArray(templateString));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().hits().length, equalTo(1));
}

7. TemplateQueryTests#testTemplateQueryAsEscapedString()

Project: elasticsearch
File: TemplateQueryTests.java
/**
     * Test that template can be expressed as a single escaped string.
     */
public void testTemplateQueryAsEscapedString() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String query = "{" + "  \"inline\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\"," + "  \"params\":{" + "    \"size\": 1" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(new BytesArray(query));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().hits().length, equalTo(1));
}

8. TemplateQueryTests#testSearchTemplateQueryFromFile()

Project: elasticsearch
File: TemplateQueryTests.java
public void testSearchTemplateQueryFromFile() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String query = "{" + "  \"file\": \"full-query-template\"," + "  \"params\":{" + "    \"mySize\": 2," + "    \"myField\": \"text\"," + "    \"myValue\": \"value1\"" + "  }" + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(new BytesArray(query));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().hits().length, equalTo(1));
}

9. TemplateQueryTests#testThatParametersCanBeSet()

Project: elasticsearch
File: TemplateQueryTests.java
public void testThatParametersCanBeSet() throws Exception {
    index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());
    index("test", "type", "2", jsonBuilder().startObject().field("theField", "foo 2").endObject());
    index("test", "type", "3", jsonBuilder().startObject().field("theField", "foo 3").endObject());
    index("test", "type", "4", jsonBuilder().startObject().field("theField", "foo 4").endObject());
    index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
    refresh();
    Map<String, Object> templateParams = new HashMap<>();
    templateParams.put("mySize", "2");
    templateParams.put("myField", "theField");
    templateParams.put("myValue", "foo");
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("full-query-template").setScriptType(ScriptType.FILE).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 4);
    // size kicks in here...
    assertThat(searchResponse.getResponse().getHits().getHits().length, is(2));
    templateParams.put("myField", "otherField");
    searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("full-query-template").setScriptType(ScriptType.FILE).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 1);
}

10. TemplateQueryTests#testSearchRequestTemplateSource()

Project: elasticsearch
File: TemplateQueryTests.java
public void testSearchRequestTemplateSource() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String query = "{ \"inline\" : { \"query\": {\"match_{{template}}\": {} } }, \"params\" : { \"template\":\"all\" } }";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(new BytesArray(query));
    request.setRequest(searchRequest);
    SearchTemplateResponse response = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertHitCount(response.getResponse(), 2);
}

11. RenderSearchTemplateTests#testFileTemplate()

Project: elasticsearch
File: RenderSearchTemplateTests.java
public void testFileTemplate() throws ExecutionException, InterruptedException {
    Map<String, Object> params = new HashMap<>();
    params.put("value", "bar");
    params.put("size", 20);
    SearchTemplateResponse response = prepareRenderSearchTemplate("file_template_1", ScriptType.FILE, params).get();
    assertThat(response, notNullValue());
    assertFalse(response.hasResponse());
    BytesReference source = response.getSource();
    assertThat(source, notNullValue());
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    assertThat(sourceAsMap, notNullValue());
    String expected = TEMPLATE_CONTENTS.replace("{{value}}", "bar").replace("{{size}}", "20");
    Map<String, Object> expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
    params = new HashMap<>();
    params.put("value", "baz");
    params.put("size", 100);
    response = prepareRenderSearchTemplate("file_template_1", ScriptType.FILE, params).get();
    assertThat(response, notNullValue());
    source = response.getSource();
    assertThat(source, notNullValue());
    sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    expected = TEMPLATE_CONTENTS.replace("{{value}}", "baz").replace("{{size}}", "100");
    expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
}

12. RenderSearchTemplateTests#testIndexedTemplate()

Project: elasticsearch
File: RenderSearchTemplateTests.java
public void testIndexedTemplate() throws ExecutionException, InterruptedException {
    Map<String, Object> params = new HashMap<>();
    params.put("value", "bar");
    params.put("size", 20);
    SearchTemplateResponse response = prepareRenderSearchTemplate("index_template_1", ScriptType.STORED, params).get();
    assertThat(response, notNullValue());
    assertFalse(response.hasResponse());
    BytesReference source = response.getSource();
    assertThat(source, notNullValue());
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    assertThat(sourceAsMap, notNullValue());
    String expected = TEMPLATE_CONTENTS.replace("{{value}}", "bar").replace("{{size}}", "20");
    Map<String, Object> expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
    params = new HashMap<>();
    params.put("value", "baz");
    params.put("size", 100);
    response = prepareRenderSearchTemplate("index_template_1", ScriptType.STORED, params).get();
    assertThat(response, notNullValue());
    source = response.getSource();
    assertThat(source, notNullValue());
    sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    expected = TEMPLATE_CONTENTS.replace("{{value}}", "baz").replace("{{size}}", "100");
    expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
}

13. RenderSearchTemplateTests#testInlineTemplate()

Project: elasticsearch
File: RenderSearchTemplateTests.java
public void testInlineTemplate() throws ExecutionException, InterruptedException {
    Map<String, Object> params = new HashMap<>();
    params.put("value", "bar");
    params.put("size", 20);
    SearchTemplateResponse response = prepareRenderSearchTemplate(TEMPLATE_CONTENTS, ScriptType.INLINE, params).get();
    assertThat(response, notNullValue());
    assertFalse(response.hasResponse());
    BytesReference source = response.getSource();
    assertThat(source, notNullValue());
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    assertThat(sourceAsMap, notNullValue());
    String expected = TEMPLATE_CONTENTS.replace("{{value}}", "bar").replace("{{size}}", "20");
    Map<String, Object> expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
    params = new HashMap<>();
    params.put("value", "baz");
    params.put("size", 100);
    response = prepareRenderSearchTemplate(TEMPLATE_CONTENTS, ScriptType.INLINE, params).get();
    assertThat(response, notNullValue());
    assertFalse(response.hasResponse());
    source = response.getSource();
    assertThat(source, notNullValue());
    sourceAsMap = XContentHelper.convertToMap(source, false).v2();
    expected = TEMPLATE_CONTENTS.replace("{{value}}", "baz").replace("{{size}}", "100");
    expectedMap = XContentHelper.convertToMap(new BytesArray(expected), false).v2();
    assertThat(sourceAsMap, equalTo(expectedMap));
}