-
-
Save 211211/6cbe9d433cdb65c979d558d9e194c1a6 to your computer and use it in GitHub Desktop.
Get all documents from Elasticsearch index by scroll method
This file contains hidden or 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
| const elasticsearch = require('elasticsearch'); | |
| const esClient = new elasticsearch.Client({ | |
| host: 'YOUR ELASTICSEARCH SERVER' | |
| }); | |
| /** | |
| * Get all documents of an Elasticsearch index by scroll method | |
| * @param {string} index Index to get documents | |
| * @param {Object} query Querying object | |
| * @param {number} size Size of each scroll. Default: 1000 | |
| */ | |
| async function getAllDocuments(index, query, size = 1000) { | |
| let data = []; | |
| const getNext = async (scrollId) => { | |
| return await esClient.scroll({ | |
| scroll: '2m', | |
| scrollId: scrollId | |
| }); | |
| } | |
| let result = await esClient.search({ | |
| index: index, | |
| size: size, | |
| scroll: '2m', | |
| body: { query: query } | |
| }); | |
| console.log('Total hits:', result.hits.total); | |
| console.log('Page hits count:', result.hits.hits.length); | |
| while (result.hits.hits.length) { | |
| console.log('Getting next', result.hits.hits.length); | |
| data = data.concat(result.hits.hits); | |
| result = await getNext(result._scroll_id); | |
| } | |
| console.log('Done', data.length); | |
| return data; | |
| } | |
| exports.getAllDocuments = getAllDocuments; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment