SDK
SDK Javascript v5.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

fetchNext #

Fetches the next SearchResult, by triggering a new search/scroll request depending on the options and filters of the SearchResult.

If the previous request was a search or a scroll action which provided a scroll argument, fetchNext will use the scrollId retrieved from the current result to make a new scroll request.

If the previous request was a search action which provided size argument and sort filtering, fetchNext will use Elasticsearch's search_after mechanism, which can efficiently search through a large volume of documents, bypassing internal hard limits[1], but at the cost of reflecting the latest changes of the index, as opposed to using scroll.

If the previous request was a search action which provided from and size arguments, fetchNext will add size to from and make a new search request.


How to process every document of a collection #

The safest way to process all documents in a collection is to fetch them as a batch in order to avoid memory exhaustion and possibly hitting some hard limits[1] on the database layer.

Make sure your first search request includes size and scroll parameters
\[1\] Elasticsearch limits the number of documents inside a single page to [10,000 by default](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/index-modules.html#dynamic-index-settings).

Usage #

Copied to clipboard!
// Using callbacks (NodeJS or Web Browser)
searchResult.fetchNext(function (error, nextSearchResult) {
  // called once the fetchNext action has been completed
  // nextSearchResult is an instantiated SearchResult object
});
// Using promises (NodeJS)
searchResult.fetchNextPromise()
  .then(nextSearchResult => {
    // called once the fetchNext action has been completed
    // nextSearchResult is an instantiated SearchResult object
  });
Copied to clipboard!
var processDocument = function (document) {
  // do something with the document
};
kuzzle
  .collection('collection', 'index')
  .search({
    from: 0,
    size: 1000,
    scroll: '30s',
    query: {}
  }, function getMoreUntilDone (error, searchResult) {
    if (searchResult === null) {
      return;
    }
    searchResult.documents.forEach(function (document) {
      processDocument(document);
    });
    searchResult.fetchNext(getMoreUntilDone);
  });