Created
April 18, 2017 17:41
-
-
Save ilovett/ded3b19af01f05f8022413067bd6275a to your computer and use it in GitHub Desktop.
elasticsearch 5.0 scroll to end scrolltoend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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