我们正在尝试在查询中使用请求超时,但它似乎对我们不起作用。
以下是我们在设置过程中所做的事情:
- search.default_allow_partial_results : false (在服务器端和客户端)
- 在我们点击的每个搜索查询中设置 10 毫秒的超时。(客户端)
- 除此之外,我们还设置了全局超时,如下面的代码所示:
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 标志。但事实并非如此。
你能指导我们哪里错了或者我们缺少什么吗?