List<DfsSearchResult> dfsResults = newArrayList();
for (ShardIterator shardIt : clusterService.operationRouting().searchShards(clusterService.state(), new String[]{"test"}, new String[]{"test"}, null, null, null)) {
for (ShardRouting shardRouting : shardIt.asUnordered()) {
InternalSearchRequest searchRequest = searchRequest(shardRouting, sourceBuilder, SearchType.DFS_QUERY_THEN_FETCH)
.scroll(new Scroll(new TimeValue(10, TimeUnit.MINUTES)));
dfsResults.add(nodeToSearchService.get(shardRouting.currentNodeId()).executeDfsPhase(searchRequest));
}
}
AggregatedDfs dfs = searchPhaseController.aggregateDfs(dfsResults);
Map<SearchShardTarget, QuerySearchResultProvider> queryResults = newHashMap();
for (DfsSearchResult dfsResult : dfsResults) {
queryResults.put(dfsResult.shardTarget(), nodeToSearchService.get(dfsResult.shardTarget().nodeId()).executeQueryPhase(new QuerySearchRequest(dfsResult.id(), dfs)));
}
ShardDoc[] sortedShardList = searchPhaseController.sortDocs(queryResults.values());
Map<SearchShardTarget, ExtTIntArrayList> docIdsToLoad = searchPhaseController.docIdsToLoad(sortedShardList);
Map<SearchShardTarget, FetchSearchResult> fetchResults = newHashMap();
for (Map.Entry<SearchShardTarget, ExtTIntArrayList> entry : docIdsToLoad.entrySet()) {
SearchShardTarget shardTarget = entry.getKey();
ExtTIntArrayList docIds = entry.getValue();
FetchSearchResult fetchResult = nodeToSearchService.get(shardTarget.nodeId()).executeFetchPhase(new FetchSearchRequest(queryResults.get(shardTarget).queryResult().id(), docIds));
fetchResults.put(fetchResult.shardTarget(), fetchResult.initCounter());
}
SearchHits hits = searchPhaseController.merge(sortedShardList, queryResults, fetchResults).hits();
assertThat(hits.totalHits(), equalTo(100l));
assertThat(hits.hits().length, equalTo(60));
for (int i = 0; i < 60; i++) {
SearchHit hit = hits.hits()[i];
// System.out.println(hit.explanation());
assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(i)));
}
// now try and scroll to the next batch of results
Map<SearchShardTarget, QuerySearchResultProvider> scollQueryResults = newHashMap();
for (QuerySearchResultProvider queryResult : queryResults.values()) {
scollQueryResults.put(queryResult.queryResult().shardTarget(), nodeToSearchService.get(queryResult.shardTarget().nodeId()).executeQueryPhase(new InternalScrollSearchRequest(queryResult.id()).scroll(new Scroll(timeValueMinutes(10)))).queryResult());
}
queryResults = scollQueryResults;
sortedShardList = searchPhaseController.sortDocs(queryResults.values());
docIdsToLoad = searchPhaseController.docIdsToLoad(sortedShardList);