[scan util] Track scroll_id across paged requests

This commit is contained in:
Jonathan Budzenski 2015-12-09 13:48:58 -06:00
parent bf292fe440
commit 8f1f1ac49e
2 changed files with 20 additions and 7 deletions

View file

@ -64,6 +64,18 @@ describe('Scanner', function () {
});
});
it('should scroll across multiple pages', function () {
scroll.restore();
let oneResult = {'took':1,'timed_out':false,'_shards':{'total':1,'successful':1,'failed':0},'hits':{'total':2,'max_score':0.0,'hits':['one']}}; // eslint-disable-line max-len
scroll = sinon.stub(scanner.client, 'scroll', (req, cb) => cb(null, oneResult));
return scanner.scanAndMap(null, {pageSize: 1})
.then(function (response) {
expect(scroll.calledTwice);
expect(response.hits.length).to.be(2);
expect(scroll.getCall(1).args[0].scrollId).to.be('abc');
expect(scroll.getCall(0).args[0].scrollId).to.be('abc');
});
});
afterEach(function () {
search.restore();

View file

@ -11,17 +11,17 @@ let Scanner = function (client, {index, type} = {}) {
};
Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
let scrollId;
let body;
let allResults = {
hits: [],
total: 0
};
const opts = _.defaults(options || {}, {
pageSize: 100,
docCount: 1000
});
let allResults = {
hits: [],
total: 0
};
let body;
if (searchString) {
body = {
query: {
@ -40,6 +40,7 @@ Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
const getMoreUntilDone = (error, response) => {
const scanAllResults = opts.docCount === Infinity;
allResults.total = scanAllResults ? response.hits.total : Math.min(response.hits.total, opts.docCount);
scrollId = response._scroll_id || scrollId;
let hits = response.hits.hits
.slice(0, allResults.total - allResults.hits.length);
@ -52,7 +53,7 @@ Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
resolve(allResults);
} else {
this.client.scroll({
scrollId: response._scroll_id,
scrollId
}, getMoreUntilDone);
}
};