Skip to content

Commit

Permalink
Track scroll_id across paged requests
Browse files Browse the repository at this point in the history
Fixes #5586
  • Loading branch information
jbudz committed Dec 9, 2015
1 parent ab0f014 commit b004690
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/ui/public/utils/__tests__/scanner.js
Expand Up @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions src/ui/public/utils/scanner.js
Expand Up @@ -11,17 +11,17 @@ let Scanner = function (client, {index, type} = {}) {
};

Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
const opts = _.defaults(options || {}, {
pageSize: 100,
docCount: 1000
});

let scrollId;
let body;
let allResults = {
hits: [],
total: 0
};
const opts = _.defaults(options || {}, {
pageSize: 100,
docCount: 1000
});

let body;
if (searchString) {
body = {
query: {
Expand All @@ -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);
Expand All @@ -52,7 +53,7 @@ Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
resolve(allResults);
} else {
this.client.scroll({
scrollId: response._scroll_id,
scrollId
}, getMoreUntilDone);
}
};
Expand Down

0 comments on commit b004690

Please sign in to comment.