return new CountResult(r.getHits().getTotalHits(), r.getTookInMillis(), r.getHits());
}
public ScrollResult scroll(String query, TimeRange range, int limit, int offset, List<String> fields, String filter) throws IndexHelper.InvalidRangeFormatException {
final Set<String> indices = IndexHelper.determineAffectedIndices(indexRangeService, deflector, range);
final SearchRequestBuilder srb = standardSearchRequest(query, indices, limit, offset, range, null, false);
if (range != null && filter != null) {
srb.setPostFilter(standardFilters(range, filter));
}
// only request the fields we asked for otherwise we can't figure out which fields will be in the result set
// until we've scrolled through the entire set.
// TODO: Check if we can get away without loading the _source field.
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html#search-request-fields
// "For backwards compatibility, if the fields parameter specifies fields which are not stored , it will
// load the _source and extract it from it. This functionality has been replaced by the source filtering
// parameter." -- So we should look at the source filtering parameter once we switched to ES 1.x.
srb.addFields(fields.toArray(new String[fields.size()]));
srb.addField("_source"); // always request the _source field because otherwise we can't access non-stored values
final SearchRequest request = srb.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(1, TimeUnit.MINUTES))
.setSize(500).request(); // TODO magic numbers
if (LOG.isDebugEnabled()) {
try {
LOG.debug("ElasticSearch scroll query: {}", XContentHelper.convertToJson(request.source(), false));