# Can't implement pagination with scroll in ElasticSearch 6.6.1

**URL:** <https://discuss.elastic.co/t/cant-implement-pagination-with-scroll-in-elasticsearch-6-6-1/170952>\
**Category:** Elasticsearch\
**Created:** [March 5, 2019, 4:34pm UTC](https://discuss.elastic.co/t/cant-implement-pagination-with-scroll-in-elasticsearch-6-6-1/170952 "2019-03-05T16:34:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Chonov](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@Stefan\_Chonov](https://discuss.elastic.co/u/Stefan_Chonov)\
**Post date:** [March 5, 2019, 4:34pm UTC](https://discuss.elastic.co/t/cant-implement-pagination-with-scroll-in-elasticsearch-6-6-1/170952/1 "2019-03-05T16:34:23Z")

</div>

I try to implement pagination with using scroll. I read this article but does not help me.  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html)

My problem is when I try to make a few requests the results are same. Scrolling is not working.

We have more than 20 billions records in the database.

My ElasticSearch version is: 6.6.1

PHP code snippet:

```
public function someFunction($client, $elasticResult, $limit)
{
    $requestQuery = {
       "scroll":"1m",
       "size":10000,
       "index":"my_index",
       "type":"my_type",
       "body":{
          "query":{
             "bool":{
                "must":{
                   "terms":{
                      "db":[ 13, 15, 19
                      ]
                   }
                }
             }
          },
          "sort":[
             "_doc"
          ]
       }
    }

    $client = $client->build();
    $response = $client->search($requestQuery);

    $elasticResult->setScrollTime('1m');

    $metadata = [];
    yield from $elasticResult->getPagingResults($response, $client, $metadata, $limit);
}

    public function getPagingResults(array $response, Client $client, &$result, int $limit = null): \Generator
    {
        $i = 0;

        while (isset($response['hits']['hits']) && \count($response['hits']['hits']) > 0) {
            foreach ($response['hits']['hits'] as $hit) {
                ++$i;

                if (null !== $limit && $i > $limit) {
                    break;
                }

                if (!empty($hit['_source'])) {
                    foreach ($hit['_source'] as $key => $value) {
                        if (!isset($result[$hit['_source']['db']][$key])) {
                            $result[$hit['_source']['db']][$key] = 0;
                        }

                        if ('' !== trim($value)) {
                            ++$result[$hit['_source']['db']][$key];
                        }
                    }
                }

                yield $hit;
            }

            if (null !== $limit && $i > $limit) {
                break;
            }

            $scrollId = $response['_scroll_id'];

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

```

If is necessary additional info, Please don't hesitate to ask.

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [March 6, 2019, 5:40am UTC](https://discuss.elastic.co/t/cant-implement-pagination-with-scroll-in-elasticsearch-6-6-1/170952/2 "2019-03-06T05:40:30Z")

</div>

> [@Stefan\_Chonov](#):
>
> PHP code snippet:

I don't use PHP myself so I can't comment on your particular example, but in general you need a scroll ID to be able to scroll through a search result.

There is a PHP example [here](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scrolling) showing the use of a scroller. Hope that helps you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 3, 2019, 5:40am UTC](https://discuss.elastic.co/t/cant-implement-pagination-with-scroll-in-elasticsearch-6-6-1/170952/3 "2019-04-03T05:40:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
