Skip to content

Latest commit

 

History

History
52 lines (41 loc) · 1.19 KB

File metadata and controls

52 lines (41 loc) · 1.19 KB
endpoint scroll
lang php
es_version 9.3
client elasticsearch/elasticsearch==9.3.0

Elasticsearch 9.3 scroll endpoint (PHP example)

Use $client->scroll() to retrieve the next batch of results from a scrolling search. Start by calling search with a scroll parameter, then repeatedly call scroll until no more hits are returned:

$response = $client->search([
    'index' => 'products',
    'scroll' => '1m',
    'size' => 2,
    'body' => ['query' => ['match_all' => new \stdClass()]],
]);

$scrollId = $response['_scroll_id'];
$hits = $response['hits']['hits'];

while (count($hits) > 0) {
    foreach ($hits as $hit) {
        echo "{$hit['_id']}: {$hit['_source']['name']}\n";
    }

    $response = $client->scroll([
        'body' => ['scroll_id' => $scrollId, 'scroll' => '1m'],
    ]);
    $scrollId = $response['_scroll_id'];
    $hits = $response['hits']['hits'];
}

Cleaning up

Always clear the scroll context when you are done to free server resources. Pass the last scroll_id you received:

$client->clearScroll(['body' => ['scroll_id' => $scrollId]]);

To clear every open scroll context at once, call without arguments:

$client->clearScroll();