FOSElasticaBundle
FOSElasticaBundle copied to clipboard
How to use search_after with PIT?
I need to use pagination and everything that I have used so far is not working for me. Last shoot was with Scroll but it was giving me issues. I decided to use search_after
by using a point in time
wonder is this two are supported within the bundle or in the underneath ruflin/Elastica
package, if so can any provide me with some examples?
If somebody is stuck how to use with PIT
+ search_after
here is a short example with generator:
public function iterate(SomeCriteria $criteria, int $size = 100): Generator
{
$pit = $this->index
->openPointInTime('3m')
->getData()['id']
?? throw new RuntimeException('Can not open PIT')
;
$query = $this
->criteriaToQuery($criteria)
->setPointInTime(new PointInTime($pit, '3m'))
->setSort(['field1', 'field2'])
->setSize($size)
;
$client = $this->index->getClient();
$search = new Search($client);
try {
do {
// PIT already contains index information -> search directly on client.
$result = $search->search($query)->getResults();
$hasNext = count($result) === $size;
yield from $result;
if ($hasNext) {
$query = $query->setParam('search_after', $result[$size - 1]->getSort());
}
} while ($hasNext);
} finally {
$client->closePointInTime($pit);
}
}