Skip to content

Instantly share code, notes, and snippets.

@ilovett
Created April 18, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilovett/ded3b19af01f05f8022413067bd6275a to your computer and use it in GitHub Desktop.
Save ilovett/ded3b19af01f05f8022413067bd6275a to your computer and use it in GitHub Desktop.
elasticsearch 5.0 scroll to end scrolltoend
fetchStuff(min, max = min) {
const body = new Bodybuilder();
body.size(3);
body.filter('range', 'timestamp', {
gte: min,
lte: max
});
return this.elasticsearch.search({
index: 'my-alias',
type: 'my-doc',
body: body.build('v2'), // have not upgraded to newer bodybuilder package yet
scroll: '15s'
})
.then((res) => this._scrollToEnd(res, []))
.then((events) => {
// sort by timestamp then _id
events = _.sortBy(events, ['_.source.timestamp', '_id']);
return events;
});
}
_scrollToEnd(res, events) {
return Promise.resolve().then(() => {
events = events.concat(_.get(res, 'hits.hits', []));
if (res.hits.total > events.length) {
return this.elasticsearch.scroll({
scrollId: res._scroll_id,
scroll: '15s'
})
.then((res) => this._scrollToEnd(res, events));
}
return events;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment