Search in sources :

Example 11 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class SearchTemplateIT method testIndexedTemplateWithArray.

public void testIndexedTemplateWithArray() throws Exception {
    String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
    assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("4").setContent(jsonBuilder().startObject().field("template", multiQuery).endObject().bytes(), 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> arrayTemplateParams = new HashMap<>();
    String[] fieldParams = { "foo", "bar" };
    arrayTemplateParams.put("fieldParam", fieldParams);
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams).get();
    assertHitCount(searchResponse.getResponse(), 5);
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 12 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest 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 13 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest 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 14 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class SearchTemplateIT method testIndexedTemplateOverwrite.

// Relates to #10397
public void testIndexedTemplateOverwrite() throws Exception {
    createIndex("testindex");
    ensureGreen("testindex");
    client().prepareIndex("testindex", "test", "1").setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()).get();
    client().admin().indices().prepareRefresh().get();
    int iterations = randomIntBetween(2, 11);
    for (int i = 1; i < iterations; i++) {
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"template\":{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"ooophrase_prefix\"}}}}}"), XContentType.JSON));
        GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "git01").get();
        assertNotNull(getResponse.getSource());
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("P_Keyword1", "dev");
        ParsingException e = expectThrows(ParsingException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
        assertThat(e.getMessage(), containsString("[match] query does not support type ooophrase_prefix"));
        assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"phrase_prefix\"}}}}"), XContentType.JSON));
        SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
        assertHitCount(searchResponse.getResponse(), 1);
        assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 15 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project elasticsearch by elastic.

the class MultiSearchTemplateIT method testBasic.

public void testBasic() throws Exception {
    createIndex("msearch");
    final int numDocs = randomIntBetween(10, 100);
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders[i] = client().prepareIndex("msearch", "test", String.valueOf(i)).setSource("odd", (i % 2 == 0), "group", (i % 3));
    }
    indexRandom(true, indexRequestBuilders);
    final String template = jsonBuilder().startObject().startObject("query").startObject("{{query_type}}").field("{{field_name}}", "{{field_value}}").endObject().endObject().endObject().string();
    MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
    // Search #1
    SearchTemplateRequest search1 = new SearchTemplateRequest();
    search1.setRequest(new SearchRequest("msearch"));
    search1.setScriptType(ScriptType.INLINE);
    search1.setScript(template);
    Map<String, Object> params1 = new HashMap<>();
    params1.put("query_type", "match");
    params1.put("field_name", "odd");
    params1.put("field_value", true);
    search1.setScriptParams(params1);
    multiRequest.add(search1);
    // Search #2 (Simulate is true)
    SearchTemplateRequest search2 = new SearchTemplateRequest();
    search2.setRequest(new SearchRequest("msearch"));
    search2.setScriptType(ScriptType.INLINE);
    search2.setScript(template);
    search2.setSimulate(true);
    Map<String, Object> params2 = new HashMap<>();
    params2.put("query_type", "match_phrase_prefix");
    params2.put("field_name", "message");
    params2.put("field_value", "quick brown f");
    search2.setScriptParams(params2);
    multiRequest.add(search2);
    // Search #3
    SearchTemplateRequest search3 = new SearchTemplateRequest();
    search3.setRequest(new SearchRequest("msearch"));
    search3.setScriptType(ScriptType.INLINE);
    search3.setScript(template);
    search3.setSimulate(false);
    Map<String, Object> params3 = new HashMap<>();
    params3.put("query_type", "term");
    params3.put("field_name", "odd");
    params3.put("field_value", "false");
    search3.setScriptParams(params3);
    multiRequest.add(search3);
    // Search #4 (Fail because of unknown index)
    SearchTemplateRequest search4 = new SearchTemplateRequest();
    search4.setRequest(new SearchRequest("unknown"));
    search4.setScriptType(ScriptType.INLINE);
    search4.setScript(template);
    Map<String, Object> params4 = new HashMap<>();
    params4.put("query_type", "match");
    params4.put("field_name", "group");
    params4.put("field_value", "test");
    search4.setScriptParams(params4);
    multiRequest.add(search4);
    // Search #5 (Simulate is true)
    SearchTemplateRequest search5 = new SearchTemplateRequest();
    search5.setRequest(new SearchRequest("msearch"));
    search5.setScriptType(ScriptType.INLINE);
    search5.setScript("{{! ignore me }}{\"query\":{\"terms\":{\"group\":[{{#groups}}{{.}},{{/groups}}]}}}");
    search5.setSimulate(true);
    Map<String, Object> params5 = new HashMap<>();
    params5.put("groups", Arrays.asList(1, 2, 3));
    search5.setScriptParams(params5);
    multiRequest.add(search5);
    MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get();
    assertThat(response.getResponses(), arrayWithSize(5));
    MultiSearchTemplateResponse.Item response1 = response.getResponses()[0];
    assertThat(response1.isFailure(), is(false));
    SearchTemplateResponse searchTemplateResponse1 = response1.getResponse();
    assertThat(searchTemplateResponse1.hasResponse(), is(true));
    assertHitCount(searchTemplateResponse1.getResponse(), (numDocs / 2) + (numDocs % 2));
    assertThat(searchTemplateResponse1.getSource().utf8ToString(), equalTo("{\"query\":{\"match\":{\"odd\":\"true\"}}}"));
    MultiSearchTemplateResponse.Item response2 = response.getResponses()[1];
    assertThat(response2.isFailure(), is(false));
    SearchTemplateResponse searchTemplateResponse2 = response2.getResponse();
    assertThat(searchTemplateResponse2.hasResponse(), is(false));
    assertThat(searchTemplateResponse2.getSource().utf8ToString(), equalTo("{\"query\":{\"match_phrase_prefix\":{\"message\":\"quick brown f\"}}}"));
    MultiSearchTemplateResponse.Item response3 = response.getResponses()[2];
    assertThat(response3.isFailure(), is(false));
    SearchTemplateResponse searchTemplateResponse3 = response3.getResponse();
    assertThat(searchTemplateResponse3.hasResponse(), is(true));
    assertHitCount(searchTemplateResponse3.getResponse(), (numDocs / 2));
    assertThat(searchTemplateResponse3.getSource().utf8ToString(), equalTo("{\"query\":{\"term\":{\"odd\":\"false\"}}}"));
    MultiSearchTemplateResponse.Item response4 = response.getResponses()[3];
    assertThat(response4.isFailure(), is(true));
    assertThat(response4.getFailure(), instanceOf(IndexNotFoundException.class));
    assertThat(response4.getFailure().getMessage(), equalTo("no such index"));
    MultiSearchTemplateResponse.Item response5 = response.getResponses()[4];
    assertThat(response5.isFailure(), is(false));
    SearchTemplateResponse searchTemplateResponse5 = response5.getResponse();
    assertThat(searchTemplateResponse5.hasResponse(), is(false));
    assertThat(searchTemplateResponse5.getSource().utf8ToString(), equalTo("{\"query\":{\"terms\":{\"group\":[1,2,3,]}}}"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) HashMap(java.util.HashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)66 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 BytesArray (org.elasticsearch.common.bytes.BytesArray)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)6 ArrayList (java.util.ArrayList)5 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 Test (org.junit.Test)5 List (java.util.List)4