Search in sources :

Example 6 with MultiSearchResponse

use of 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 7 with MultiSearchResponse

use of 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 8 with MultiSearchResponse

use of 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.elasticsearch.action.search.MultiSearchResponse)8 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Item (org.elasticsearch.action.get.MultiGetRequest.Item)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 Test (org.junit.Test)2 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)1 MultiSearchRequestBuilder (org.elasticsearch.action.search.MultiSearchRequestBuilder)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 ScriptScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder)1 Script (org.elasticsearch.script.Script)1