Search in sources :

Example 11 with MultiSearchResponse

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

the class PercolatorQuerySearchIT method testPercolatorQueryViaMultiSearch.

public void testPercolatorQueryViaMultiSearch() throws Exception {
    createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type", "field1", "type=text").addMapping("queries", "query", "type=percolator"));
    client().prepareIndex("test", "queries", "1").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "2").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "3").setSource(jsonBuilder().startObject().field("query", boolQuery().must(matchQuery("field1", "b")).must(matchQuery("field1", "c"))).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "4").setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()).execute().actionGet();
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("field1", "c").endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().get();
    MultiSearchResponse response = client().prepareMultiSearch().add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", jsonBuilder().startObject().field("field1", "b").endObject().bytes(), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", yamlBuilder().startObject().field("field1", "c").endObject().bytes(), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", smileBuilder().startObject().field("field1", "b c").endObject().bytes(), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", jsonBuilder().startObject().field("field1", "d").endObject().bytes(), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "1", null, null, null))).add(// non existing doc, so error element
    client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "2", null, null, null))).get();
    MultiSearchResponse.Item item = response.getResponses()[0];
    assertHitCount(item.getResponse(), 2L);
    assertSearchHits(item.getResponse(), "1", "4");
    assertThat(item.getFailureMessage(), nullValue());
    item = response.getResponses()[1];
    assertHitCount(item.getResponse(), 2L);
    assertSearchHits(item.getResponse(), "2", "4");
    assertThat(item.getFailureMessage(), nullValue());
    item = response.getResponses()[2];
    assertHitCount(item.getResponse(), 4L);
    assertSearchHits(item.getResponse(), "1", "2", "3", "4");
    assertThat(item.getFailureMessage(), nullValue());
    item = response.getResponses()[3];
    assertHitCount(item.getResponse(), 1L);
    assertSearchHits(item.getResponse(), "4");
    assertThat(item.getFailureMessage(), nullValue());
    item = response.getResponses()[4];
    assertHitCount(item.getResponse(), 2L);
    assertSearchHits(item.getResponse(), "2", "4");
    assertThat(item.getFailureMessage(), nullValue());
    item = response.getResponses()[5];
    assertThat(item.getResponse(), nullValue());
    assertThat(item.getFailureMessage(), notNullValue());
    assertThat(item.getFailureMessage(), equalTo("all shards failed"));
    assertThat(ExceptionsHelper.unwrapCause(item.getFailure().getCause()).getMessage(), containsString("[test/type/2] couldn't be found"));
}
Also used : MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 12 with MultiSearchResponse

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

the class IndicesOptionsIntegrationIT method verify.

private static void verify(ActionRequestBuilder requestBuilder, boolean fail, long expectedCount) {
    if (fail) {
        if (requestBuilder instanceof MultiSearchRequestBuilder) {
            MultiSearchResponse multiSearchResponse = ((MultiSearchRequestBuilder) requestBuilder).get();
            assertThat(multiSearchResponse.getResponses().length, equalTo(1));
            assertThat(multiSearchResponse.getResponses()[0].isFailure(), is(true));
            assertThat(multiSearchResponse.getResponses()[0].getResponse(), nullValue());
        } else {
            try {
                requestBuilder.get();
                fail("IndexNotFoundException or IndexClosedException was expected");
            } catch (IndexNotFoundException | IndexClosedException e) {
            }
        }
    } else {
        if (requestBuilder instanceof SearchRequestBuilder) {
            SearchRequestBuilder searchRequestBuilder = (SearchRequestBuilder) requestBuilder;
            assertHitCount(searchRequestBuilder.get(), expectedCount);
        } else if (requestBuilder instanceof MultiSearchRequestBuilder) {
            MultiSearchResponse multiSearchResponse = ((MultiSearchRequestBuilder) requestBuilder).get();
            assertThat(multiSearchResponse.getResponses().length, equalTo(1));
            assertThat(multiSearchResponse.getResponses()[0].getResponse(), notNullValue());
        } else {
            requestBuilder.get();
        }
    }
}
Also used : MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder)

Example 13 with MultiSearchResponse

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

the class TransportTwoNodesSearchIT method testFailedMultiSearchWithWrongQuery.

public void testFailedMultiSearchWithWrongQuery() throws Exception {
    prepareData();
    logger.info("Start Testing failed multi search with a wrong query");
    MultiSearchResponse response = client().prepareMultiSearch().add(client().prepareSearch("test").setQuery(new MatchQueryBuilder("foo", "biz"))).add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))).add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())).execute().actionGet();
    assertThat(response.getResponses().length, equalTo(3));
    assertThat(response.getResponses()[0].getFailureMessage(), notNullValue());
    assertThat(response.getResponses()[1].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1));
    assertThat(response.getResponses()[2].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10));
    logger.info("Done Testing failed search");
}
Also used : MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder)

Example 14 with MultiSearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiSearch.

@Test
public void testMultiSearch() throws Exception {
    //first, INDEX two values
    Map<String, Object> headers = new HashMap<String, Object>();
    node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
    node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
    //now, verify MULTISEARCH
    headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.MULTISEARCH);
    SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx"));
    SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy"));
    SearchRequestBuilder srb3 = node.client().prepareSearch("instagram").setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue"));
    List<SearchRequest> list = new ArrayList<>();
    list.add(srb1.request());
    list.add(srb2.request());
    list.add(srb3.request());
    MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class);
    MultiSearchResponse.Item[] responses = response.getResponses();
    assertNotNull("response should not be null", response);
    assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length);
    assertFalse("response 1 should be ok", responses[0].isFailure());
    assertFalse("response 2 should be ok", responses[1].isFailure());
    assertTrue("response 3 should be failed", responses[2].isFailure());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) Item(org.elasticsearch.action.get.MultiGetRequest.Item) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with MultiSearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiSearch.

@Test
public void testMultiSearch() throws Exception {
    //first, INDEX two values
    Map<String, Object> headers = new HashMap<String, Object>();
    node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
    node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
    //now, verify MULTISEARCH
    headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTISEARCH);
    SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx"));
    SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy"));
    SearchRequestBuilder srb3 = node.client().prepareSearch("instagram").setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue"));
    List<SearchRequest> list = new ArrayList<>();
    list.add(srb1.request());
    list.add(srb2.request());
    list.add(srb3.request());
    MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class);
    MultiSearchResponse.Item[] responses = response.getResponses();
    assertNotNull("response should not be null", response);
    assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length);
    assertFalse("response 1 should be ok", responses[0].isFailure());
    assertFalse("response 2 should be ok", responses[1].isFailure());
    assertTrue("response 3 should be failed", responses[2].isFailure());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) Item(org.elasticsearch.action.get.MultiGetRequest.Item) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)13 Test (org.junit.Test)10 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)8 SearchConfig (org.graylog.plugins.views.search.engine.SearchConfig)8 QueryResult (org.graylog.plugins.views.search.QueryResult)6 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Arrays (java.util.Arrays)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Provider (javax.inject.Provider)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 Query (org.graylog.plugins.views.search.Query)3 SearchJob (org.graylog.plugins.views.search.SearchJob)3 SearchType (org.graylog.plugins.views.search.SearchType)3 IndexLookup (org.graylog.plugins.views.search.elasticsearch.IndexLookup)3 QueryStringDecorators (org.graylog.plugins.views.search.elasticsearch.QueryStringDecorators)3 SearchTypeError (org.graylog.plugins.views.search.errors.SearchTypeError)3