Skip to content

Instantly share code, notes, and snippets.

@211211
Forked from johnnybui/elasticsearch-scroll.js
Created February 22, 2022 18:12
Show Gist options
  • Select an option

  • Save 211211/6cbe9d433cdb65c979d558d9e194c1a6 to your computer and use it in GitHub Desktop.

Select an option

Save 211211/6cbe9d433cdb65c979d558d9e194c1a6 to your computer and use it in GitHub Desktop.
Get all documents from Elasticsearch index by scroll method
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