Search in sources :

Example 16 with MultiSearchResponse

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);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) DockerImageName(org.testcontainers.utility.DockerImageName) IndexResponse(org.opensearch.action.index.IndexResponse) SimpleFormatter(java.util.logging.SimpleFormatter) ToXContent(org.opensearch.common.xcontent.ToXContent) ClusterRerouteAction(org.opensearch.action.admin.cluster.reroute.ClusterRerouteAction) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) ClusterRerouteRequest(org.opensearch.action.admin.cluster.reroute.ClusterRerouteRequest) FieldMappingMetadata(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetadata) ShrinkAction(org.opensearch.action.admin.indices.shrink.ShrinkAction) AfterAll(org.junit.jupiter.api.AfterAll) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Alias(org.opensearch.action.admin.indices.alias.Alias) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) GetResponse(org.opensearch.action.get.GetResponse) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) PendingClusterTasksResponse(org.opensearch.action.admin.cluster.tasks.PendingClusterTasksResponse) TimeValue(org.opensearch.common.unit.TimeValue) GetPipelineResponse(org.opensearch.action.ingest.GetPipelineResponse) SearchHit(org.opensearch.search.SearchHit) GetMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse) Result(org.opensearch.action.DocWriteResponse.Result) RolloverResponse(org.opensearch.action.admin.indices.rollover.RolloverResponse) Settings(org.opensearch.common.settings.Settings) Logger(java.util.logging.Logger) RestStatus(org.opensearch.rest.RestStatus) StandardCharsets(java.nio.charset.StandardCharsets) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Test(org.junit.jupiter.api.Test) ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) GetIndexResponse(org.opensearch.action.admin.indices.get.GetIndexResponse) GetFieldMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) BytesArray(org.opensearch.common.bytes.BytesArray) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse) RefreshResponse(org.opensearch.action.admin.indices.refresh.RefreshResponse) ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) XContentType(org.opensearch.common.xcontent.XContentType) CurlResponse(org.codelibs.curl.CurlResponse) RefreshPolicy(org.opensearch.action.support.WriteRequest.RefreshPolicy) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BytesReference(org.opensearch.common.bytes.BytesReference) UpdateResponse(org.opensearch.action.update.UpdateResponse) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) Level(java.util.logging.Level) ClusterUpdateSettingsResponse(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) AnalyzeAction(org.opensearch.action.admin.indices.analyze.AnalyzeAction) DeleteResponse(org.opensearch.action.delete.DeleteResponse) AliasesExistResponse(org.opensearch.action.admin.indices.alias.exists.AliasesExistResponse) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) GenericContainer(org.testcontainers.containers.GenericContainer) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SearchResponse(org.opensearch.action.search.SearchResponse) SyncedFlushResponse(org.opensearch.action.admin.indices.flush.SyncedFlushResponse) OutputStream(java.io.OutputStream) QueryBuilders(org.opensearch.index.query.QueryBuilders) ExplainResponse(org.opensearch.action.explain.ExplainResponse) FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) MainAction(org.opensearch.action.main.MainAction) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) ForceMergeResponse(org.opensearch.action.admin.indices.forcemerge.ForceMergeResponse) MainRequest(org.opensearch.action.main.MainRequest) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) Curl(org.codelibs.curl.Curl) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) AfterEach(org.junit.jupiter.api.AfterEach) OpenIndexResponse(org.opensearch.action.admin.indices.open.OpenIndexResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) MainResponse(org.opensearch.action.main.MainResponse) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) HttpNodesStatsAction(org.codelibs.fesen.client.action.HttpNodesStatsAction) ConsoleHandler(java.util.logging.ConsoleHandler) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) MultiGetRequestBuilder(org.opensearch.action.get.MultiGetRequestBuilder) ClearIndicesCacheResponse(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Test(org.junit.jupiter.api.Test)

Example 17 with MultiSearchResponse

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

Example 18 with MultiSearchResponse

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

Example 19 with MultiSearchResponse

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

Example 20 with MultiSearchResponse

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

Aggregations

MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)22 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)7 SearchResponse (org.opensearch.action.search.SearchResponse)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 SearchRequest (org.opensearch.action.search.SearchRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 StandardCharsets (java.nio.charset.StandardCharsets)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ConsoleHandler (java.util.logging.ConsoleHandler)3 Level (java.util.logging.Level)3 Logger (java.util.logging.Logger)3 SimpleFormatter (java.util.logging.SimpleFormatter)3 Curl (org.codelibs.curl.Curl)3 CurlResponse (org.codelibs.curl.CurlResponse)3 HttpNodesStatsAction (org.codelibs.fesen.client.action.HttpNodesStatsAction)3 ActionListener (org.opensearch.action.ActionListener)3 TimeValue (org.opensearch.common.unit.TimeValue)3