Search in sources :

Example 16 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 17 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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 18 with SearchRequest

use of org.graylog.shaded.elasticsearch7.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)

Example 19 with SearchRequest

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

the class RestDeleteByQueryAction method buildRequest.

@Override
protected DeleteByQueryRequest buildRequest(RestRequest request) throws IOException {
    if (false == request.hasContent()) {
        throw new ElasticsearchException("_delete_by_query requires a request body");
    }
    /*
         * Passing the search request through DeleteByQueryRequest first allows
         * it to set its own defaults which differ from SearchRequest's
         * defaults. Then the parseInternalRequest can override them.
         */
    DeleteByQueryRequest internal = new DeleteByQueryRequest(new SearchRequest());
    Map<String, Consumer<Object>> consumers = new HashMap<>();
    consumers.put("conflicts", o -> internal.setConflicts((String) o));
    parseInternalRequest(internal, request, consumers);
    return internal;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap) DeleteByQueryRequest(org.elasticsearch.action.bulk.byscroll.DeleteByQueryRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 20 with SearchRequest

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

the class RestUpdateByQueryAction method buildRequest.

@Override
@SuppressWarnings("unchecked")
protected UpdateByQueryRequest buildRequest(RestRequest request) throws IOException {
    /*
         * Passing the search request through UpdateByQueryRequest first allows
         * it to set its own defaults which differ from SearchRequest's
         * defaults. Then the parse can override them.
         */
    UpdateByQueryRequest internal = new UpdateByQueryRequest(new SearchRequest());
    Map<String, Consumer<Object>> consumers = new HashMap<>();
    consumers.put("conflicts", o -> internal.setConflicts((String) o));
    consumers.put("script", o -> internal.setScript(parseScript((Map<String, Object>) o)));
    parseInternalRequest(internal, request, consumers);
    internal.setPipeline(request.param("pipeline"));
    return internal;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)156 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)81 SearchResponse (org.elasticsearch.action.search.SearchResponse)69 Test (org.junit.Test)37 IOException (java.io.IOException)31 SearchHit (org.elasticsearch.search.SearchHit)25 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)22 ArrayList (java.util.ArrayList)21 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)21 HashMap (java.util.HashMap)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 List (java.util.List)16 SearchHits (org.elasticsearch.search.SearchHits)15 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Map (java.util.Map)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)11 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10