A library to deep paginate an Elasticsearch search operation. There are three ways to paginate:
Which one to use depends on the context, read more in the Elasticsearch documentation.
The library will get pageSize
amount of hits in memory at the same time, which means a lower amount will result in less memory used but more requests to Elasticsearch (and the opposite). Never will it fully exhaust
an index before returning the results.
The first step is to construct an $elasticsearchClient
(instance of Elasticsearch\Client
) which you can read more about in the Elasticsearch official PHP driver.
use Nekman\EsPagination\CursorFactories\EsScrollCursorFactory;
$cursorFactory = new EsScrollCursorFactory(
$elasticsearchClient,
$pageSize = 1000,
$scrollDuration = "1m"
);
$params = [
/*
* Same params as a normal Elasticsearch search operation.
* See Elasticsearch documentation for more information.
*/
];
$cursor = $cursorFactory->hits($params);
foreach ($cursor as $hit) {
echo "Hit {$hit['_id']}";
}
use Nekman\EsPagination\CursorFactories\EsFromCursorFactory;
$cursorFactory = new EsFromCursorFactory(
$elasticsearchClient,
$pageSize = 1000
);
$params = [
/*
* Same params as a normal Elasticsearch search operation.
* See Elasticsearch documentation for more information.
*/
];
$cursor = $cursorFactory->hits($params);
foreach ($cursor as $hit) {
echo "Hit {$hit['_id']}";
}
use Nekman\EsPagination\CursorFactories\EsSearchAfterCursorFactory;
$cursorFactory = new EsSearchAfterCursorFactory(
$elasticsearchClient,
$pageSize = 1000
);
$params = [
/*
* Same params as a normal Elasticsearch search operation.
* See Elasticsearch documentation for more information.
*/
];
$cursor = $cursorFactory->hits($params);
foreach ($cursor as $hit) {
echo "Hit {$hit['_id']}";
}
Elasticsearch pit (point in time) is a lightweight view into the state of the data as it existed when initiated. Create a cursor factory and decorate it with PIT:
use \Nekman\EsPagination\CursorFactories\EsPitCursorFactory;
$cursorFactory = /* Create cursor factory, see above */;
$pitCursorFactory = new EsPitCursorFactory(
$cursorFactory,
$elasticsearchFactory,
$pitKeepAlive = "1m"
);
$params = [
/*
* Same params as a normal Elasticsearch search operation.
* See Elasticsearch documentation for more information.
*/
];
$cursor = $cursorFactory->hits($params);
foreach ($cursor as $hit) {
echo "Hit {$hit['_id']}";
}
This project complies with Semantic Versioning.
For a complete list of changes, and how to migrate between major versions, see releases page.