use of org.opensearch.action.search.MultiSearchResponse in project fesen-httpclient by codelibs.
the class OpenSearchClientTest method test_multi_search.
@Test
void test_multi_search() throws Exception {
final SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("word")).setSize(1);
final SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "test")).setSize(1);
CountDownLatch latch = new CountDownLatch(1);
client.prepareMultiSearch().add(srb1).add(srb2).execute(wrap(res -> {
long nbHits = 0;
for (MultiSearchResponse.Item item : res.getResponses()) {
SearchResponse searchResponse = item.getResponse();
nbHits += searchResponse.getHits().getTotalHits().value;
}
assertEquals(0, nbHits);
latch.countDown();
}, e -> {
e.printStackTrace();
latch.countDown();
try {
fail();
} finally {
latch.countDown();
}
}));
latch.await();
{
MultiSearchResponse res = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();
long nbHits = 0;
for (MultiSearchResponse.Item item : res.getResponses()) {
SearchResponse searchResponse = item.getResponse();
nbHits += searchResponse.getHits().getTotalHits().value;
}
assertEquals(0, nbHits);
}
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchIT method testMultiSearch.
public void testMultiSearch() throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
SearchRequest searchRequest1 = new SearchRequest("index1");
searchRequest1.source().sort("id", SortOrder.ASC);
multiSearchRequest.add(searchRequest1);
SearchRequest searchRequest2 = new SearchRequest("index2");
searchRequest2.source().sort("id", SortOrder.ASC);
multiSearchRequest.add(searchRequest2);
SearchRequest searchRequest3 = new SearchRequest("index3");
searchRequest3.source().sort("id", SortOrder.ASC);
multiSearchRequest.add(searchRequest3);
MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync);
assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L));
assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3));
assertThat(multiSearchResponse.getResponses()[0].getFailure(), Matchers.nullValue());
assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(false));
SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse());
assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, Matchers.equalTo(2L));
assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("1"));
assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(1).getId(), Matchers.equalTo("2"));
assertThat(multiSearchResponse.getResponses()[1].getFailure(), Matchers.nullValue());
assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(false));
SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[1].getResponse());
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getTotalHits().value, Matchers.equalTo(2L));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("3"));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(1).getId(), Matchers.equalTo("4"));
assertThat(multiSearchResponse.getResponses()[2].getFailure(), Matchers.nullValue());
assertThat(multiSearchResponse.getResponses()[2].isFailure(), Matchers.is(false));
SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[2].getResponse());
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getTotalHits().value, Matchers.equalTo(2L));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("5"));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(1).getId(), Matchers.equalTo("6"));
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchIT method testMultiSearch_failure.
public void testMultiSearch_failure() throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
SearchRequest searchRequest1 = new SearchRequest("index1");
searchRequest1.source().query(new ScriptQueryBuilder(new Script(ScriptType.INLINE, "invalid", "code", Collections.emptyMap())));
multiSearchRequest.add(searchRequest1);
SearchRequest searchRequest2 = new SearchRequest("index2");
searchRequest2.source().query(new ScriptQueryBuilder(new Script(ScriptType.INLINE, "invalid", "code", Collections.emptyMap())));
multiSearchRequest.add(searchRequest2);
MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync);
assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L));
assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(2));
assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(true));
assertThat(multiSearchResponse.getResponses()[0].getFailure().getMessage(), containsString("search_phase_execution_exception"));
assertThat(multiSearchResponse.getResponses()[0].getResponse(), nullValue());
assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(true));
assertThat(multiSearchResponse.getResponses()[1].getFailure().getMessage(), containsString("search_phase_execution_exception"));
assertThat(multiSearchResponse.getResponses()[1].getResponse(), nullValue());
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class PercolatorQuerySearchIT method testPercolatorQueryViaMultiSearch.
public void testPercolatorQueryViaMultiSearch() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test").addMapping("type", "field1", "type=text", "query", "type=percolator"));
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject()).execute().actionGet();
client().prepareIndex("test").setId("2").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject()).execute().actionGet();
client().prepareIndex("test").setId("3").setSource(jsonBuilder().startObject().field("query", boolQuery().must(matchQuery("field1", "b")).must(matchQuery("field1", "c"))).endObject()).execute().actionGet();
client().prepareIndex("test").setId("4").setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()).execute().actionGet();
client().prepareIndex("test").setId("5").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", BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", BytesReference.bytes(yamlBuilder().startObject().field("field1", "c").endObject()), XContentType.YAML))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", BytesReference.bytes(jsonBuilder().startObject().field("field1", "b c").endObject()), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", BytesReference.bytes(jsonBuilder().startObject().field("field1", "d").endObject()), XContentType.JSON))).add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "test", "type", "5", null, null, null))).add(// non existing doc, so error element
client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "test", "type", "6", 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(), containsString("[test/type/6] couldn't be found"));
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchCancellationIT method testCancelMultiSearch.
public void testCancelMultiSearch() throws Exception {
List<ScriptedBlockPlugin> plugins = initBlockFactory();
indexTestData();
ActionFuture<MultiSearchResponse> msearchResponse = client().prepareMultiSearch().add(client().prepareSearch("test").addScriptField("test_field", new Script(ScriptType.INLINE, "mockscript", SCRIPT_NAME, Collections.emptyMap()))).execute();
awaitForBlock(plugins);
cancelSearch(MultiSearchAction.NAME);
disableBlocks(plugins);
for (MultiSearchResponse.Item item : msearchResponse.actionGet()) {
if (item.getFailure() != null) {
assertThat(ExceptionsHelper.unwrap(item.getFailure(), TaskCancelledException.class), notNullValue());
} else {
assertFailures(item.getResponse());
for (ShardSearchFailure shardFailure : item.getResponse().getShardFailures()) {
assertThat(ExceptionsHelper.unwrap(shardFailure.getCause(), TaskCancelledException.class), notNullValue());
}
}
}
}
Aggregations