I am querying elasticsearch with the below query
SearchResponse searchResponse = getElasticSearchClient()
.prepareSearch(indexNameStr).setTypes(typeNameStr)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryStrr).setSize(size)
.setFrom(start).addSort(fieldSortBuilder)
.execute().actionGet();
I wanted to check if the search is success or failure. Catch exception If there are any during search.
Is it correct to use below method to identify failures in search;
public void parseSearchResultFailures(SearchResponse res) {
if (res.getFailedShards() > 0) {
final ShardSearchFailure[] shardFailures = res.getShardFailures();
StringBuilder reasonBuilder = new StringBuilder();
for (ShardSearchFailure failure : shardFailures) {
final String reason = failure.reason();
LOGGER.info("Status: " + failure.status()
+ " - Search failed on shard: " + reason);
reasonBuilder.append(reason);
}
throw new ElasticsearchException("Search failed: "
+ reasonBuilder.toString());
}
}
Thanks,