Java Code Examples for org.elasticsearch.action.search.SearchResponse#getShardFailures()

The following examples show how to use org.elasticsearch.action.search.SearchResponse#getShardFailures() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchSupportHandler.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Override
public List<T> findAll(SearchRequest searchRequest) throws Exception {
	SearchResponse searchResp = this.restHighLevelClient.search(searchRequest);
	for (ShardSearchFailure failure : searchResp.getShardFailures()) {
		listener.onFailure(failure);
	}
	SearchHits hits = searchResp.getHits();
	SearchHit[] searchHits = hits.getHits();
	List<T> list = new ArrayList<>();
	for (SearchHit hit : searchHits) {
		String sourceAsString = hit.getSourceAsString();
		T t = JacksonUtils.parseJSON(sourceAsString, clazzP);
		list.add(t);
	}
	Collections.reverse(list);
	return list;
}
 
Example 2
Source File: Generator.java    From elasticsearch-report-engine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param response
 * @return
 */
@SuppressWarnings("unchecked")
public List<Map> extractData(SearchResponse response)
    throws NoDataFoundException, ReportGenerationException {
  List<Map> data = new LinkedList<>();
  SearchHits hits = response.getHits();
  if (response.getShardFailures().length > 0) {
    throw new ReportGenerationException("Report failed to get data. Kindly try again.");
  }
  if (hits.getTotalHits() == 0) {
    throw new NoDataFoundException("No data found");
  }
  try {
    for (SearchHit hit : hits) {
      Map<String, Object> sourceMap = hit.getSourceAsMap();
      data.add(sourceMap);
    }
  } catch (Exception e) {
    throw new NoDataFoundException("Error extracting data : " + e.getMessage());
  }
  return data;
}
 
Example 3
Source File: DefaultRequestHandler.java    From elasticsearch-taste with Apache License 2.0 6 votes vote down vote up
protected void validateRespose(final SearchResponse response) {
    final int totalShards = response.getTotalShards();
    final int successfulShards = response.getSuccessfulShards();
    if (totalShards != successfulShards) {
        throw new MissingShardsException(totalShards - successfulShards
                + " shards are failed.");
    }
    final ShardSearchFailure[] failures = response.getShardFailures();
    if (failures.length > 0) {
        final StringBuilder buf = new StringBuilder();
        for (final ShardOperationFailedException failure : failures) {
            buf.append('\n').append(failure.toString());
        }
        throw new OperationFailedException("Search Operation Failed: "
                + buf.toString());
    }
}
 
Example 4
Source File: SearchResponseUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public static SearchResponse checkForFailures(SearchResponse searchResponse) {
    ShardSearchFailure[] shardFailures = searchResponse.getShardFailures();
    if (shardFailures.length > 0) {
        for (ShardSearchFailure shardFailure : shardFailures) {
            LOGGER.error("search response shard failure", shardFailure.getCause());
        }
        throw new VertexiumException("search response shard failures", shardFailures[0].getCause());
    }
    return searchResponse;
}
 
Example 5
Source File: SearchResponseUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public static SearchResponse checkForFailures(SearchResponse searchResponse) {
    ShardSearchFailure[] shardFailures = searchResponse.getShardFailures();
    if (shardFailures.length > 0) {
        for (ShardSearchFailure shardFailure : shardFailures) {
            LOGGER.error("search response shard failure", shardFailure.getCause());
        }
        throw new VertexiumException("search response shard failures", shardFailures[0].getCause());
    }
    return searchResponse;
}
 
Example 6
Source File: ConfService.java    From SkaETL with Apache License 2.0 4 votes vote down vote up
private void treatError(SearchResponse searchResponse) {
    log.error("Pwoblem when load configuration From ES");
    for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
        log.error(failure.toString());
    }
}
 
Example 7
Source File: EsHighLevelRestSearchTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
     * @return void
     * @Author pancm
     * @Description 普通查询
     * @Date 2019/9/12
     * @Param []
     **/
    private static void genSearch() throws IOException {
        String type = "_doc";
        String index = "test1";
        // 查询指定的索引库
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 设置查询条件
        sourceBuilder.query(QueryBuilders.termQuery("uid", "1234"));
        // 设置起止和结束
        sourceBuilder.from(0);
        sourceBuilder.size(5);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // 设置路由
//		searchRequest.routing("routing");
        // 设置索引库表达式
        searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
        // 查询选择本地分片,默认是集群分片
        searchRequest.preference("_local");

        // 排序
        // 根据默认值进行降序排序
//		sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
        // 根据字段进行升序排序
//		sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));

        // 关闭suorce查询
//		sourceBuilder.fetchSource(false);

        String[] includeFields = new String[]{"title", "user", "innerObject.*"};
        String[] excludeFields = new String[]{"_type"};
        // 包含或排除字段
//		sourceBuilder.fetchSource(includeFields, excludeFields);

        searchRequest.source(sourceBuilder);
        System.out.println("普通查询的DSL语句:"+sourceBuilder.toString());
        // 同步查询
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // HTTP状态代码、执行时间或请求是否提前终止或超时
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();

        // 供关于受搜索影响的切分总数的统计信息,以及成功和失败的切分
        int totalShards = searchResponse.getTotalShards();
        int successfulShards = searchResponse.getSuccessfulShards();
        int failedShards = searchResponse.getFailedShards();
        // 失败的原因
        for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
            // failures should be handled here
        }
        // 结果
        searchResponse.getHits().forEach(hit -> {
            Map<String, Object> map = hit.getSourceAsMap();
            System.out.println("普通查询的结果:" + map);
        });
        System.out.println("\n=================\n");
    }