{"action":"closed","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch-php/issues/258","labels_url":"https://api.github.com/repos/elastic/elasticsearch-php/issues/258/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch-php/issues/258/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch-php/issues/258/events","html_url":"https://github.com/elastic/elasticsearch-php/issues/258","id":91853288,"number":258,"title":"Abstracting scrolled responses with PHP Iterators","user":{"login":"arturom","id":4431562,"avatar_url":"https://avatars.githubusercontent.com/u/4431562?v=3","gravatar_id":"","url":"https://api.github.com/users/arturom","html_url":"https://github.com/arturom","followers_url":"https://api.github.com/users/arturom/followers","following_url":"https://api.github.com/users/arturom/following{/other_user}","gists_url":"https://api.github.com/users/arturom/gists{/gist_id}","starred_url":"https://api.github.com/users/arturom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arturom/subscriptions","organizations_url":"https://api.github.com/users/arturom/orgs","repos_url":"https://api.github.com/users/arturom/repos","events_url":"https://api.github.com/users/arturom/events{/privacy}","received_events_url":"https://api.github.com/users/arturom/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-06-29T16:41:27Z","updated_at":"2015-12-15T19:28:53Z","closed_at":"2015-12-15T19:28:53Z","body":"Currently, it's possible to [scroll through search results](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scan_scroll) using `$client->search()` and then `$client->scroll()` \r\n\r\nThis pattern has come up repeatedly for me to export data in a background process. It would be nice to have a way to abstract it. A  solution would be to use PHP's [Iterator interface](http://www.php.net/manual/en/class.iterator.php). I've come up with two separate iterator classes: One iterator would be used iterate over every \"page\" of results and one to iterate over every individual hit.\r\n\r\n```php\r\n$client = new Elasticsearch\\Client($config);\r\n\r\n$search_params = array(\r\n    'search_type' => 'scan',\r\n    'scroll'      => '5m',\r\n    'index'       => 'twitter',\r\n    'size'        => 1000,\r\n    'body'        => array(\r\n        'query' => array(\r\n            'match_all' => new StdClass\r\n        )\r\n    )\r\n);\r\n\r\n$pages = new Elasticsearch\\Iterators\\PageIterator($client, $search_params);\r\n$hits = new Elasticsearch\\Iterators\\HitIterator($pages);\r\n\r\n// Sample usage of iterating over hits\r\nforeach($hits as $hit) {\r\n    // do something with this hit. For example: write to a CSV, update a database, etc..\r\n    echo $hit['_id'], PHP_EOL;\r\n}\r\n// Sample usage of iterating over scrolled responses\r\nforeach($pages as $page) {\r\n    // do something with this hit. For example: copy its data to another index, etc...\r\n    echo count($page['hits']['hits']), PHP_EOL;\r\n}\r\n\r\n```\r\nI have an implementation of these classes in my forked repo: https://github.com/arturom/elasticsearch-php/commits/iterators\r\n\r\nDo you think these classes have a place in this library?"}}