fetchNext

// 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
  });
searchResult.fetchNext(new ResponseListener<SearchResult>() {
  @Override
  public void onSuccess(SearchResult nextSearchResult) {
    // called once the fetchNext action has been completed
    // nextSearchResult is an instantiated SearchResult object
  }

  @Override
  public void onError(JSONObject error) {
    // Handle error
  }
});
<?php

use \Kuzzle\SearchResult;

// ...

/**
 * @var $searchResult SearchResult
 */

try {
  $nextSearchResult = $searchResult->fetchNext();
} catch (ErrorException $e) {
    // Handle error
}

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 document, 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

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);
  });
import io.kuzzle.sdk.core.Kuzzle;
import io.kuzzle.sdk.core.Options;

Kuzzle kuzzle = new Kuzzle("localhost");

JSONObject filter = new JSONObject();

Options options = new Options();
options.setFrom((long) 0);
options.setSize((long) 1000);
options.setScroll("30s");

ResponseListener<SearchResult> listener = new ResponseListener<SearchResult>() {
  @Override
  public void onSuccess(SearchResult searchResult) {
    if (searchResult == null) {
      return;
    }

    for (Document doc : searchResult.getDocuments()) {
      // do something with the document
      // this.processDocument(doc);
    }

    searchResult.fetchNext(this);
  }

  @Override
  public void onError(JSONObject error) {
    // handle errors here
  }
};

kuzzle
  .collection("collection", "index")
  .search(filter, options, listener);
<?php

use \Kuzzle\Kuzzle;
use \Kuzzle\Document;
use \Kuzzle\Util\SearchResult;

function processDocument ($doc) {
  // do something with the document
}

$kuzzle = new Kuzzle('localhost');
$collection = $kuzzle->collection('collection', 'index');

try {
  $searchResult = $collection->search([
    'from' => 0,
    'size' => 1000,
    'scroll' => '30s',
    'filter' => []
  ]);

  while ($searchResult !== null) {
    foreach ($searchResult->getDocuments() as $doc) {
      procesDocument($doc);
    }

    $searchResult = $searchResult->fetchNext();
  }
}
catch (ErrorException $e) {
  // Handle errors here
}

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