Search in sources :

Example 36 with BytesArray

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));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray)

Example 37 with BytesArray

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"));
    }
}
Also used : Script(org.elasticsearch.script.Script) BytesArray(org.elasticsearch.common.bytes.BytesArray) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 38 with BytesArray

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>"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 39 with BytesArray

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");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 40 with BytesArray

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) RestRequest(org.elasticsearch.rest.RestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Aggregations

BytesArray (org.elasticsearch.common.bytes.BytesArray)203 BytesReference (org.elasticsearch.common.bytes.BytesReference)36 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)29 StreamInput (org.elasticsearch.common.io.stream.StreamInput)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)21 HashMap (java.util.HashMap)17 BytesRef (org.apache.lucene.util.BytesRef)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)14 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)13 ArrayList (java.util.ArrayList)12 TopDocs (org.apache.lucene.search.TopDocs)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 Document (org.elasticsearch.index.mapper.ParseContext.Document)12 Index (org.elasticsearch.index.Index)11 Map (java.util.Map)10 IndexableField (org.apache.lucene.index.IndexableField)10 IndexService (org.elasticsearch.index.IndexService)10