use of 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"));
}
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();
}
}
}
use of 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");
}
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());
}
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());
}
Aggregations