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