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);
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());
}
hits = searchPhaseController.merge(sortedShardList, queryResults, fetchResults).hits();
assertThat(hits.totalHits(), equalTo(100l));
assertThat(hits.hits().length, equalTo(40));
for (int i = 0; i < 40; i++) {
SearchHit hit = hits.hits()[i];
assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(i + 60)));
}
// now try and scroll to the next next batch of results
scollQueryResults = newHashMap();
for (QuerySearchResultProvider queryResult : queryResults.values()) {
scollQueryResults.put(queryResult.queryResult().shardTarget(), nodeToSearchService.get(queryResult.shardTarget().nodeId()).executeQueryPhase(new InternalScrollSearchRequest(queryResult.id())).queryResult());
}
queryResults = scollQueryResults;
sortedShardList = searchPhaseController.sortDocs(queryResults.values());
docIdsToLoad = searchPhaseController.docIdsToLoad(sortedShardList);
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());
}
hits = searchPhaseController.merge(sortedShardList, queryResults, fetchResults).hits();
assertThat(hits.totalHits(), equalTo(100l));
assertThat(hits.hits().length, equalTo(0));