use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchIT method testMultiSearch_withAgg.
public void testMultiSearch_withAgg() throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
SearchRequest searchRequest1 = new SearchRequest("index1");
searchRequest1.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)));
multiSearchRequest.add(searchRequest1);
SearchRequest searchRequest2 = new SearchRequest("index2");
searchRequest2.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)));
multiSearchRequest.add(searchRequest2);
SearchRequest searchRequest3 = new SearchRequest("index3");
searchRequest3.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)));
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().getHits().length, Matchers.equalTo(0));
Terms terms = multiSearchResponse.getResponses()[0].getResponse().getAggregations().get("name");
assertThat(terms.getBuckets().size(), Matchers.equalTo(2));
assertThat(terms.getBuckets().get(0).getKeyAsString(), Matchers.equalTo("value1"));
assertThat(terms.getBuckets().get(1).getKeyAsString(), Matchers.equalTo("value2"));
assertThat(multiSearchResponse.getResponses()[1].getFailure(), Matchers.nullValue());
assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(false));
SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse());
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getTotalHits().value, Matchers.equalTo(2L));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getHits().length, Matchers.equalTo(0));
terms = multiSearchResponse.getResponses()[1].getResponse().getAggregations().get("name");
assertThat(terms.getBuckets().size(), Matchers.equalTo(2));
assertThat(terms.getBuckets().get(0).getKeyAsString(), Matchers.equalTo("value1"));
assertThat(terms.getBuckets().get(1).getKeyAsString(), Matchers.equalTo("value2"));
assertThat(multiSearchResponse.getResponses()[2].getFailure(), Matchers.nullValue());
assertThat(multiSearchResponse.getResponses()[2].isFailure(), Matchers.is(false));
SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse());
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getTotalHits().value, Matchers.equalTo(2L));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getHits().length, Matchers.equalTo(0));
terms = multiSearchResponse.getResponses()[2].getResponse().getAggregations().get("name");
assertThat(terms.getBuckets().size(), Matchers.equalTo(2));
assertThat(terms.getBuckets().get(0).getKeyAsString(), Matchers.equalTo("value1"));
assertThat(terms.getBuckets().get(1).getKeyAsString(), Matchers.equalTo("value2"));
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchIT method testMultiSearch_withQuery.
public void testMultiSearch_withQuery() throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
SearchRequest searchRequest1 = new SearchRequest("index1");
searchRequest1.source().query(new TermsQueryBuilder("field", "value2"));
multiSearchRequest.add(searchRequest1);
SearchRequest searchRequest2 = new SearchRequest("index2");
searchRequest2.source().query(new TermsQueryBuilder("field", "value2"));
multiSearchRequest.add(searchRequest2);
SearchRequest searchRequest3 = new SearchRequest("index3");
searchRequest3.source().query(new TermsQueryBuilder("field", "value2"));
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(1L));
assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).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(1L));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).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(1L));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("6"));
searchRequest1.source().highlighter(new HighlightBuilder().field("field"));
searchRequest2.source().highlighter(new HighlightBuilder().field("field"));
searchRequest3.source().highlighter(new HighlightBuilder().field("field"));
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(1L));
assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
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(1L));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("4"));
assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
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(1L));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("6"));
assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), Matchers.equalTo("<em>value2</em>"));
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testMultiSearch.
public void testMultiSearch() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
{
// tag::multi-search-request-basic
// <1>
MultiSearchRequest request = new MultiSearchRequest();
// <2>
SearchRequest firstSearchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("user", "foobar"));
firstSearchRequest.source(searchSourceBuilder);
// <3>
request.add(firstSearchRequest);
// <4>
SearchRequest secondSearchRequest = new SearchRequest();
searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("user", "quxx"));
secondSearchRequest.source(searchSourceBuilder);
request.add(secondSearchRequest);
// end::multi-search-request-basic
// tag::multi-search-execute
MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
// end::multi-search-execute
// tag::multi-search-response
// <1>
MultiSearchResponse.Item firstResponse = response.getResponses()[0];
// <2>
assertNull(firstResponse.getFailure());
// <3>
SearchResponse searchResponse = firstResponse.getResponse();
assertEquals(4, searchResponse.getHits().getTotalHits().value);
// <4>
MultiSearchResponse.Item secondResponse = response.getResponses()[1];
assertNull(secondResponse.getFailure());
searchResponse = secondResponse.getResponse();
assertEquals(1, searchResponse.getHits().getTotalHits().value);
// end::multi-search-response
// tag::multi-search-execute-listener
ActionListener<MultiSearchResponse> listener = new ActionListener<MultiSearchResponse>() {
@Override
public void onResponse(MultiSearchResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-search-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-search-execute-async
// <1>
client.msearchAsync(request, RequestOptions.DEFAULT, listener);
// end::multi-search-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class MultiSearchTemplateResponse method fromXContext.
public static MultiSearchTemplateResponse fromXContext(XContentParser parser) {
// The MultiSearchTemplateResponse is identical to the multi search response so we reuse the parsing logic in multi search response
MultiSearchResponse mSearchResponse = MultiSearchResponse.fromXContext(parser);
org.opensearch.action.search.MultiSearchResponse.Item[] responses = mSearchResponse.getResponses();
Item[] templateResponses = new Item[responses.length];
int i = 0;
for (org.opensearch.action.search.MultiSearchResponse.Item item : responses) {
SearchTemplateResponse stResponse = null;
if (item.getResponse() != null) {
stResponse = new SearchTemplateResponse();
stResponse.setResponse(item.getResponse());
}
templateResponses[i++] = new Item(stResponse, item.getFailure());
}
return new MultiSearchTemplateResponse(templateResponses, mSearchResponse.getTook().millis());
}
use of org.opensearch.action.search.MultiSearchResponse in project OpenSearch by opensearch-project.
the class TransportTwoNodesSearchIT method testFailedMultiSearchWithWrongQueryWithFunctionScore.
public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception {
prepareData();
logger.info("Start Testing failed multi search with a wrong query");
MultiSearchResponse response = client().prepareMultiSearch().add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1), new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, "bar", "foo", Collections.emptyMap()))))).add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))).add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())).get();
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");
}
Aggregations