Search code examples
node.jselasticsearchexpress-4

Elasticsearch show all results using scroll in node js


I am basically trying to show all records of an index type. Now, if you use match_all() in query elasticsearch shows 10 results by default. One can show all results using scroll. I am trying to implement scroll api, but can't get it to work. It is showing only 10 results, my code:

module.exports.searchAll = function (searchData, callback) {

client.search({
    index: 'test',
    type: 'records',
    scroll: '10s',
    //search_type: 'scan', //if I use search_type then it requires size otherwise it shows 0 result
    body: {
        query: {
            "match_all": {}
        }
    }
}, function (err, resp) {
    client.scroll({
        scrollId: resp._scroll_id,
        scroll: '10s'
    }, callback(resp.hits.hits));
});
}

Can anyone help, please?


Solution

  • You need to repeatedly call client.scroll until no more records are returned. There's a good example in the elasticsearch documentation. I've reproduced their example code below, slightly modified to match your question

    var allRecords = [];
    
    // first we do a search, and specify a scroll timeout
    client.search({
      index: 'test',
      type: 'records',
      scroll: '10s',
      body: {
         query: {
             "match_all": {}
         }
      }
    }, function getMoreUntilDone(error, response) {
      // collect all the records
      response.body.hits.hits.forEach(function (hit) {
        allRecords.push(hit);
      });
    
      if (response.body.hits.total.value !== allRecords.length) {
        // now we can call scroll over and over
        client.scroll({
          scroll_id: response.body._scroll_id,
          scroll: '10s'
        }, getMoreUntilDone);
      } else {
        console.log('all done', allRecords);
      }
    });