Search in sources :

Example 1 with MultiSearchResponse

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"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) RareTermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.RareTermsAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) RareTerms(org.opensearch.search.aggregations.bucket.terms.RareTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest)

Example 2 with MultiSearchResponse

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>"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 3 with MultiSearchResponse

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));
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Item(org.opensearch.script.mustache.MultiSearchTemplateResponse.Item) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest)

Example 4 with MultiSearchResponse

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());
}
Also used : MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse)

Example 5 with MultiSearchResponse

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");
}
Also used : MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Script(org.opensearch.script.Script) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder)

Aggregations

MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)17 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)6 SearchRequest (org.opensearch.action.search.SearchRequest)5 Script (org.opensearch.script.Script)5 HashSet (java.util.HashSet)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 ActionListener (org.opensearch.action.ActionListener)2 TimeValue (org.opensearch.common.unit.TimeValue)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1