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));
}
}
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);
}
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"));
}
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));
}
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);
}
}
}
Aggregations