use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class SimpleValidateQueryIT method testIrrelevantPropertiesBeforeQuery.
public void testIrrelevantPropertiesBeforeQuery() throws IOException {
createIndex("test");
ensureGreen();
refresh();
assertThat(client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.wrapperQuery(new BytesArray("{\"foo\": \"bar\", \"query\": {\"term\" : { \"user\" : \"kimchy\" }}}"))).get().isValid(), equalTo(false));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class StoredExpressionTests method testAllOpsDisabledIndexedScripts.
public void testAllOpsDisabledIndexedScripts() throws IOException {
client().admin().cluster().preparePutStoredScript().setLang(ExpressionScriptEngineService.NAME).setId("script1").setContent(new BytesArray("{\"script\":\"2\"}"), XContentType.JSON).get();
client().prepareIndex("test", "scriptTest", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON).get();
try {
client().prepareUpdate("test", "scriptTest", "1").setScript(new Script(ScriptType.STORED, ExpressionScriptEngineService.NAME, "script1", Collections.emptyMap())).get();
fail("update script should have been rejected");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("failed to execute script"));
assertThat(e.getCause().getMessage(), containsString("scripts of type [stored], operation [update] and lang [expression] are not supported"));
}
try {
client().prepareSearch().setSource(new SearchSourceBuilder().scriptField("test1", new Script(ScriptType.STORED, "expression", "script1", Collections.emptyMap()))).setIndices("test").setTypes("scriptTest").get();
fail("search script should have been rejected");
} catch (Exception e) {
assertThat(e.toString(), containsString("scripts of type [stored], operation [search] and lang [expression] are disabled"));
}
try {
client().prepareSearch("test").setSource(new SearchSourceBuilder().aggregation(AggregationBuilders.terms("test").script(new Script(ScriptType.STORED, "expression", "script1", Collections.emptyMap())))).get();
} catch (Exception e) {
assertThat(e.toString(), containsString("scripts of type [stored], operation [aggs] and lang [expression] are disabled"));
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class SearchTemplateIT method testIndexedTemplateClient.
public void testIndexedTemplateClient() throws Exception {
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("testTemplate").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("testTemplate").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "testTemplate").get();
assertNotNull(getResponse.getSource());
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
client().admin().indices().prepareRefresh().get();
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.getSource());
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 stored script format [/template_index/mustache/1000] use only <id>"));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class SearchTemplateIT method testIndexedTemplate.
public void testIndexedTemplate() throws Exception {
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("1a").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("2").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("3").setContent(new BytesArray("{" + "\"template\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}"), XContentType.JSON));
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
client().admin().indices().prepareRefresh().get();
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("fieldParam", "foo");
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("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(ScriptType.STORED).setScriptParams(templateParams).get();
assertHitCount(searchResponse.getResponse(), 1);
assertWarnings("use of </lang/id> [/mustache/2] for looking up" + " stored scripts/templates has been deprecated, use only <id> [2] instead");
Map<String, Object> vars = new HashMap<>();
vars.put("fieldParam", "bar");
TemplateQueryBuilder builder = new TemplateQueryBuilder("3", ScriptType.STORED, vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 1);
assertWarnings("[template] query is deprecated, use search template api instead");
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class MultiSearchTemplateRequestTests method testParseWithCarriageReturn.
public void testParseWithCarriageReturn() throws Exception {
final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" + "{\"inline\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n";
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON).build();
MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true);
assertThat(request.requests().size(), equalTo(1));
assertThat(request.requests().get(0).getRequest().indices()[0], equalTo("test0"));
assertThat(request.requests().get(0).getRequest().indices()[1], equalTo("test1"));
assertThat(request.requests().get(0).getRequest().indices(), arrayContaining("test0", "test1"));
assertThat(request.requests().get(0).getRequest().requestCache(), equalTo(true));
assertThat(request.requests().get(0).getRequest().preference(), nullValue());
assertNotNull(request.requests().get(0).getScript());
assertEquals(ScriptType.INLINE, request.requests().get(0).getScriptType());
assertEquals("{\"query\":{\"match_{{template}}\":{}}}", request.requests().get(0).getScript());
assertEquals(1, request.requests().get(0).getScriptParams().size());
}
Aggregations