Search in sources :

Example 11 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class CountResponseTests method createShardFailureTestItem.

@SuppressWarnings("Duplicates")
private static ShardSearchFailure createShardFailureTestItem() {
    String randomMessage = randomAlphaOfLengthBetween(3, 20);
    Exception ex = new ParsingException(0, 0, randomMessage, new IllegalArgumentException("some bad argument"));
    SearchShardTarget searchShardTarget = null;
    if (randomBoolean()) {
        String nodeId = randomAlphaOfLengthBetween(5, 10);
        String indexName = randomAlphaOfLengthBetween(5, 10);
        searchShardTarget = new SearchShardTarget(nodeId, new ShardId(new Index(indexName, IndexMetadata.INDEX_UUID_NA_VALUE), randomInt()), null, null);
    }
    return new ShardSearchFailure(ex, searchShardTarget);
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ParsingException(org.opensearch.common.ParsingException) SearchShardTarget(org.opensearch.search.SearchShardTarget) Index(org.opensearch.index.Index) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) IOException(java.io.IOException) ParsingException(org.opensearch.common.ParsingException)

Example 12 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class CountResponseTests method assertEqualInstances.

private void assertEqualInstances(CountResponse expectedInstance, CountResponse newInstance) {
    assertEquals(expectedInstance.getCount(), newInstance.getCount());
    assertEquals(expectedInstance.status(), newInstance.status());
    assertEquals(expectedInstance.isTerminatedEarly(), newInstance.isTerminatedEarly());
    assertEquals(expectedInstance.getTotalShards(), newInstance.getTotalShards());
    assertEquals(expectedInstance.getFailedShards(), newInstance.getFailedShards());
    assertEquals(expectedInstance.getSkippedShards(), newInstance.getSkippedShards());
    assertEquals(expectedInstance.getSuccessfulShards(), newInstance.getSuccessfulShards());
    assertEquals(expectedInstance.getShardFailures().length, newInstance.getShardFailures().length);
    ShardSearchFailure[] expectedFailures = expectedInstance.getShardFailures();
    ShardSearchFailure[] newFailures = newInstance.getShardFailures();
    for (int i = 0; i < newFailures.length; i++) {
        ShardSearchFailure parsedFailure = newFailures[i];
        ShardSearchFailure originalFailure = expectedFailures[i];
        assertEquals(originalFailure.index(), parsedFailure.index());
        assertEquals(originalFailure.shard(), parsedFailure.shard());
        assertEquals(originalFailure.shardId(), parsedFailure.shardId());
        String originalMsg = originalFailure.getCause().getMessage();
        assertEquals(parsedFailure.getCause().getMessage(), "OpenSearch exception [type=parsing_exception, reason=" + originalMsg + "]");
        String nestedMsg = originalFailure.getCause().getCause().getMessage();
        assertEquals(parsedFailure.getCause().getCause().getMessage(), "OpenSearch exception [type=illegal_argument_exception, reason=" + nestedMsg + "]");
    }
}
Also used : ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 13 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testSearch.

@SuppressWarnings({ "unused", "unchecked" })
public void testSearch() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::search-request-basic
        // <1>
        SearchRequest searchRequest = new SearchRequest();
        // <2>
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // <3>
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // <4>
        searchRequest.source(searchSourceBuilder);
    // end::search-request-basic
    }
    {
        // tag::search-request-indices
        // <1>
        SearchRequest searchRequest = new SearchRequest("posts");
        // end::search-request-indices
        // tag::search-request-routing
        // <1>
        searchRequest.routing("routing");
        // end::search-request-routing
        // tag::search-request-indicesOptions
        // <1>
        searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
        // end::search-request-indicesOptions
        // tag::search-request-preference
        // <1>
        searchRequest.preference("_local");
        // end::search-request-preference
        assertNotNull(client.search(searchRequest, RequestOptions.DEFAULT));
    }
    {
        // tag::search-source-basics
        // <1>
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // <2>
        sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
        // <3>
        sourceBuilder.from(0);
        // <4>
        sourceBuilder.size(5);
        // <5>
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // end::search-source-basics
        // tag::search-source-sorting
        // <1>
        sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        // <2>
        sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
        // end::search-source-sorting
        // tag::search-source-filtering-off
        sourceBuilder.fetchSource(false);
        // end::search-source-filtering-off
        // tag::search-source-filtering-includes
        String[] includeFields = new String[] { "title", "innerObject.*" };
        String[] excludeFields = new String[] { "user" };
        sourceBuilder.fetchSource(includeFields, excludeFields);
        // end::search-source-filtering-includes
        sourceBuilder.fetchSource(true);
        // tag::search-source-setter
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("posts");
        searchRequest.source(sourceBuilder);
        // end::search-source-setter
        // tag::search-execute
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        // end::search-execute
        // tag::search-execute-listener
        ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::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::search-execute-async
        // <1>
        client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
        // end::search-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
        // tag::search-response-1
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();
        // end::search-response-1
        // tag::search-response-2
        int totalShards = searchResponse.getTotalShards();
        int successfulShards = searchResponse.getSuccessfulShards();
        int failedShards = searchResponse.getFailedShards();
        for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
        // failures should be handled here
        }
        // end::search-response-2
        assertNotNull(searchResponse);
        // tag::search-hits-get
        SearchHits hits = searchResponse.getHits();
        // end::search-hits-get
        // tag::search-hits-info
        TotalHits totalHits = hits.getTotalHits();
        // the total number of hits, must be interpreted in the context of totalHits.relation
        long numHits = totalHits.value;
        // whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        // end::search-hits-info
        // tag::search-hits-singleHit
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
        // do something with the SearchHit
        }
        // end::search-hits-singleHit
        for (SearchHit hit : searchHits) {
            // tag::search-hits-singleHit-properties
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            // end::search-hits-singleHit-properties
            // tag::search-hits-singleHit-source
            String sourceAsString = hit.getSourceAsString();
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String documentTitle = (String) sourceAsMap.get("title");
            List<Object> users = (List<Object>) sourceAsMap.get("user");
            Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
        // end::search-hits-singleHit-source
        }
        assertEquals(3, numHits);
        assertEquals(TotalHits.Relation.EQUAL_TO, relation);
        assertNotNull(hits.getHits()[0].getSourceAsString());
        assertNotNull(hits.getHits()[0].getSourceAsMap().get("title"));
        assertNotNull(hits.getHits()[0].getSourceAsMap().get("innerObject"));
        assertNull(hits.getHits()[0].getSourceAsMap().get("user"));
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchHit(org.opensearch.search.SearchHit) RatedSearchHit(org.opensearch.index.rankeval.RatedSearchHit) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) 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) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RestStatus(org.opensearch.rest.RestStatus) ArrayList(java.util.ArrayList) List(java.util.List) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) SearchHits(org.opensearch.search.SearchHits) Map(java.util.Map) HashMap(java.util.HashMap) TimeValue(org.opensearch.common.unit.TimeValue)

Example 14 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure 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)

Example 15 with ShardSearchFailure

use of org.opensearch.action.search.ShardSearchFailure in project OpenSearch by opensearch-project.

the class SearchScrollIT method testRestartDataNodesDuringScrollSearch.

public void testRestartDataNodesDuringScrollSearch() throws Exception {
    final String dataNode = internalCluster().startDataOnlyNode();
    createIndex("demo", Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put("index.routing.allocation.include._name", dataNode).build());
    createIndex("prod", Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put("index.routing.allocation.include._name", dataNode).build());
    int numDocs = randomIntBetween(10, 100);
    for (int i = 0; i < numDocs; i++) {
        index("demo", "_doc", "demo-" + i, Collections.emptyMap());
        index("prod", "_doc", "prod-" + i, Collections.emptyMap());
    }
    client().admin().indices().prepareRefresh().get();
    SearchResponse respFromDemoIndex = client().prepareSearch("demo").setSize(randomIntBetween(1, 10)).setQuery(new MatchAllQueryBuilder()).setScroll(TimeValue.timeValueMinutes(5)).get();
    internalCluster().restartNode(dataNode, new InternalTestCluster.RestartCallback());
    ensureGreen("demo", "prod");
    SearchResponse respFromProdIndex = client().prepareSearch("prod").setSize(randomIntBetween(1, 10)).setQuery(new MatchAllQueryBuilder()).setScroll(TimeValue.timeValueMinutes(5)).get();
    assertNoFailures(respFromProdIndex);
    SearchPhaseExecutionException error = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearchScroll(respFromDemoIndex.getScrollId()).get());
    for (ShardSearchFailure shardSearchFailure : error.shardFailures()) {
        assertThat(shardSearchFailure.getCause().getMessage(), containsString("No search context found for id [1]"));
    }
    client().prepareSearchScroll(respFromProdIndex.getScrollId()).get();
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) InternalTestCluster(org.opensearch.test.InternalTestCluster) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Aggregations

ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)23 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)8 SearchResponse (org.opensearch.action.search.SearchResponse)7 ParsingException (org.opensearch.common.ParsingException)7 IOException (java.io.IOException)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ShardId (org.opensearch.index.shard.ShardId)4 SearchShardTarget (org.opensearch.search.SearchShardTarget)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)3 Index (org.opensearch.index.Index)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)2 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)2 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)2