我们试图在查询中使用请求超时,但它似乎不适合我们。
以下是我们在安装过程中所做的工作:
RestHighLevelClient client = new
RestHighLevelClient(RestClient.builder(httpHost).setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(30000)
.setConnectionRequestTimeout(90000)
.setSocketTimeout(90000)).setMaxRetryTimeoutMillis(90000));
查询耗时超过8秒,但仍未超时。我们已经禁用了部分结果,希望得到一个超时错误,但我们也没有得到任何错误。
此外,即使查询花费的时间超过了指定的超时时间,isTimedOut标志也始终返回为false。
这是我正在查询的请求示例:
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
QueryBuilder infraQueryBuilder = QueryBuilders.termQuery("field1", field1);
QueryBuilder totalCountRangeQueryBuilder = QueryBuilders.rangeQuery("field2").gte(3);
BoolQueryBuilder innerBoolQueryBuilder = QueryBuilders.boolQuery();
innerBoolQueryBuilder.must(QueryBuilders.rangeQuery("nestedDocType1.field1").gt(2));
QueryBuilder filter = QueryBuilders
.nestedQuery("nestedDocType1", innerBoolQueryBuilder, ScoreMode.Max)
.innerHit(new InnerHitBuilder()
.setFetchSourceContext(
new FetchSourceContext(true, new String[]{"nestedDocType1.field1"}, null))
.addSort(SortBuilders.fieldSort("nestedDocType1.field1").order(SortOrder.DESC))
.setSize(1)
);
boolQueryBuilder.must(infraQueryBuilder);
boolQueryBuilder.must(totalCountRangeQueryBuilder);
if (inputRevisions != null && (inputRevisions.size() > 0)) {
QueryBuilder allEligibleRevisionsFilter = QueryBuilders
.termsQuery("field3", inputRevisions);
boolQueryBuilder.must(allEligibleRevisionsFilter);
}
boolQueryBuilder.filter(filter);
sourceBuilder.query(boolQueryBuilder)
.fetchSource(new String[]{
"field3",
"field2"
}, null);
sourceBuilder.size(batchSize);
sourceBuilder.timeout(TimeValue.timeValueMillis(10));
SearchRequest searchRequest = createSearchRequest(sourceBuilder, enterpriseId);
searchRequest.allowPartialSearchResults(false);
SearchResponse searchResponse = getSearchResponse(searchRequest);
ESCustomScroll<Set<String>> esCustomScroll = this::populateProcessedRevisionsSetWithESScroll;
getESDataByScroll(esCustomScroll, searchResponse, processedRevisions); // gets the data by scrolling over again and again until data is available.
这是我们用于滚动的代码:
private boolean populateProcessedRevisionsSetWithESScroll(SearchResponse searchResponse, Set<String> processedRevisions) {
if(searchResponse == null ||
searchResponse.getHits() == null ||
searchResponse.getHits().getHits() == null ||
searchResponse.getHits().getHits().length == 0) {
return false;
}
for(SearchHit outerHit : searchResponse.getHits().getHits()) {
Map<String, Object> outerSourceMap = outerHit.getSourceAsMap();
String revision = (String) outerSourceMap.get("field4");
int totalCount = (Integer) outerSourceMap.get("field3");
SearchHit[] innerHits = outerHit.getInnerHits().get("nestedDocType1").getHits();
if(innerHits == null || innerHits.length == 0) {
logger.error("No inner hits found for revision: "+revision);
continue;
}
Map<String, Object> innerSourceMap = innerHits[0].getSourceAsMap();
int simCount = (Integer) innerSourceMap.get("field1");
if(((totalCount - simCount) == 0) || (simCount > ((totalCount - simCount) / 2))) {
processedRevisions.add(revision);
}
}
return true;
}
即使在部分结果的情况下,我们也希望设置isTimedOut标志。但事实并非如此。
你能告诉我们哪里错了,我们错过了什么吗?
相关问题:虽然设置了超时,但Java高级Rest客户端没有释放连接
尝试为RestClientBuilder设置setMaxRetryTimeoutMillis–它将创建一个侦听器,并在setMaxRetryTimeoutMillis过期后将其切断。