Search in sources :

Example 1 with ShardSearchFailure

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

the class RemoteResponseParsersTests method testFailureWithoutIndex.

/**
 * Check that we can parse shard search failures without index information.
 */
public void testFailureWithoutIndex() throws IOException {
    ShardSearchFailure failure = new ShardSearchFailure(new OpenSearchRejectedExecutionException("exhausted"));
    XContentBuilder builder = jsonBuilder();
    failure.toXContent(builder, ToXContent.EMPTY_PARAMS);
    try (XContentParser parser = createParser(builder)) {
        ScrollableHitSource.SearchFailure parsed = RemoteResponseParsers.SEARCH_FAILURE_PARSER.parse(parser, null);
        assertNotNull(parsed.getReason());
        assertThat(parsed.getReason().getMessage(), Matchers.containsString("exhausted"));
        assertThat(parsed.getReason(), Matchers.instanceOf(OpenSearchRejectedExecutionException.class));
    }
}
Also used : OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) ScrollableHitSource(org.opensearch.index.reindex.ScrollableHitSource) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ShardSearchFailure

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

the class FieldSortIT method testIgnoreUnmapped.

public void testIgnoreUnmapped() throws Exception {
    createIndex("test");
    client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("id", "1").field("i_value", -1).field("d_value", -1.1).endObject()).get();
    logger.info("--> sort with an unmapped field, verify it fails");
    try {
        SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("kkk")).get();
        assertThat("Expected exception but returned with", result, nullValue());
    } catch (SearchPhaseExecutionException e) {
        // we check that it's a parse failure rather than a different shard failure
        for (ShardSearchFailure shardSearchFailure : e.shardFailures()) {
            assertThat(shardSearchFailure.toString(), containsString("[No mapping found for [kkk] in order to sort on]"));
        }
    }
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("kkk").unmappedType("keyword")).get();
    assertNoFailures(searchResponse);
    // nested field
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword").setNestedSort(new NestedSortBuilder("nested").setNestedSort(new NestedSortBuilder("nested.foo")))).get();
    assertNoFailures(searchResponse);
    // nestedQuery
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort("nested.foo").unmappedType("keyword").setNestedSort(new NestedSortBuilder("nested").setFilter(QueryBuilders.termQuery("nested.foo", "abc")))).get();
    assertNoFailures(searchResponse);
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with ShardSearchFailure

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

the class SimpleSortIT method testDocumentsWithNullValue.

public void testDocumentsWithNullValue() throws Exception {
    // TODO: sort shouldn't fail when sort field is mapped dynamically
    // We have to specify mapping explicitly because by the time search is performed dynamic mapping might not
    // be propagated to all nodes yet and sort operation fail when the sort field is not defined
    String mapping = Strings.toString(jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("svalue").field("type", "keyword").endObject().endObject().endObject().endObject());
    assertAcked(prepareCreate("test").addMapping("type1", mapping, XContentType.JSON));
    ensureGreen();
    client().prepareIndex("test").setSource(jsonBuilder().startObject().field("id", "1").field("svalue", "aaa").endObject()).get();
    client().prepareIndex("test").setSource(jsonBuilder().startObject().field("id", "2").nullField("svalue").endObject()).get();
    client().prepareIndex("test").setSource(jsonBuilder().startObject().field("id", "3").field("svalue", "bbb").endObject()).get();
    flush();
    refresh();
    Script scripField = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['id'].value", Collections.emptyMap());
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("id", scripField).addSort("svalue", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L));
    assertThat(searchResponse.getHits().getAt(0).field("id").getValue(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).field("id").getValue(), equalTo("3"));
    assertThat(searchResponse.getHits().getAt(2).field("id").getValue(), equalTo("2"));
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("id", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['id'][0]", Collections.emptyMap())).addSort("svalue", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L));
    assertThat(searchResponse.getHits().getAt(0).field("id").getValue(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).field("id").getValue(), equalTo("3"));
    assertThat(searchResponse.getHits().getAt(2).field("id").getValue(), equalTo("2"));
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addScriptField("id", scripField).addSort("svalue", SortOrder.DESC).get();
    if (searchResponse.getFailedShards() > 0) {
        logger.warn("Failed shards:");
        for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
            logger.warn("-> {}", shardSearchFailure);
        }
    }
    assertThat(searchResponse.getFailedShards(), equalTo(0));
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L));
    assertThat(searchResponse.getHits().getAt(0).field("id").getValue(), equalTo("3"));
    assertThat(searchResponse.getHits().getAt(1).field("id").getValue(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(2).field("id").getValue(), equalTo("2"));
    // a query with docs just with null values
    searchResponse = client().prepareSearch().setQuery(termQuery("id", "2")).addScriptField("id", scripField).addSort("svalue", SortOrder.DESC).get();
    if (searchResponse.getFailedShards() > 0) {
        logger.warn("Failed shards:");
        for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) {
            logger.warn("-> {}", shardSearchFailure);
        }
    }
    assertThat(searchResponse.getFailedShards(), equalTo(0));
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
    assertThat(searchResponse.getHits().getAt(0).field("id").getValue(), equalTo("2"));
}
Also used : Script(org.opensearch.script.Script) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with ShardSearchFailure

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

the class RejectionActionIT method testSimulatedSearchRejectionLoad.

public void testSimulatedSearchRejectionLoad() throws Throwable {
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test").setId(Integer.toString(i)).setSource("field", "1").get();
    }
    int numberOfAsyncOps = randomIntBetween(200, 700);
    final CountDownLatch latch = new CountDownLatch(numberOfAsyncOps);
    final CopyOnWriteArrayList<Object> responses = new CopyOnWriteArrayList<>();
    for (int i = 0; i < numberOfAsyncOps; i++) {
        client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field", "1")).execute(new LatchedActionListener<>(new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
                responses.add(searchResponse);
            }

            @Override
            public void onFailure(Exception e) {
                responses.add(e);
            }
        }, latch));
    }
    latch.await();
    // validate all responses
    for (Object response : responses) {
        if (response instanceof SearchResponse) {
            SearchResponse searchResponse = (SearchResponse) response;
            for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
                assertThat(failure.reason().toLowerCase(Locale.ENGLISH), anyOf(containsString("cancelled"), containsString("rejected")));
            }
        } else {
            Exception t = (Exception) response;
            Throwable unwrap = ExceptionsHelper.unwrapCause(t);
            if (unwrap instanceof SearchPhaseExecutionException) {
                SearchPhaseExecutionException e = (SearchPhaseExecutionException) unwrap;
                for (ShardSearchFailure failure : e.shardFailures()) {
                    assertThat(failure.reason().toLowerCase(Locale.ENGLISH), anyOf(containsString("cancelled"), containsString("rejected")));
                }
            } else if ((unwrap instanceof OpenSearchRejectedExecutionException) == false) {
                throw new AssertionError("unexpected failure", (Throwable) response);
            }
        }
    }
    assertThat(responses.size(), equalTo(numberOfAsyncOps));
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchResponse(org.opensearch.action.search.SearchResponse) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with ShardSearchFailure

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

the class SearchCancellationIT method verifyCancellationException.

private void verifyCancellationException(ShardSearchFailure[] failures) {
    for (ShardSearchFailure searchFailure : failures) {
        // failure may happen while executing the search or while sending shard request for next phase.
        // Below assertion is handling both the cases
        final Throwable topFailureCause = searchFailure.getCause();
        assertTrue(searchFailure.toString(), topFailureCause instanceof TransportException || topFailureCause instanceof TaskCancelledException);
        if (topFailureCause instanceof TransportException) {
            assertTrue(topFailureCause.getCause() instanceof TaskCancelledException);
        }
    }
}
Also used : ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) TransportException(org.opensearch.transport.TransportException)

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