# How to use Scroll in Rails/Ruby to get more than 10,000 records?

**URL:** <https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737>\
**Category:** Elasticsearch\
**Created:** [April 1, 2019, 8:38am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737 "2019-04-01T08:38:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unavailabl3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unavailabl3/32/59938_2.png) [@unavailabl3](https://discuss.elastic.co/u/unavailabl3)\
**Post date:** [April 1, 2019, 8:38am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737/1 "2019-04-01T08:38:10Z")

</div>

I need to search in my 25,000 records. But elasticsearch allow only 10,000 (from and size is the same). How can I make search and get results more than 10,000?  
There is scroll api, but I don't understand how to use it.  
EXAMPLE: result = Product.search('red',{scroll: '5m'}) .records  
How to iterate after it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 1, 2019, 8:53am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737/2 "2019-04-01T08:53:28Z")

</div>

Have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html)

You can optionally modify `index.max_result_window` index settings to `25000` if you really need it.  
But this comes with a memory pressure price. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.7/index-modules.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/index-modules.html)

As the documentation says, you can use:

- [https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-request-scroll.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-request-scroll.html)
- [https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-request-search-after.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-request-search-after.html)

About your question about scroll, the documentation I just linked to describes how it works. Is there anything unclear in it?

---

<div class="post-metadata">

**Author:** ![unavailabl3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unavailabl3/32/59938_2.png) [@unavailabl3](https://discuss.elastic.co/u/unavailabl3)\
**Post date:** [April 1, 2019, 11:10am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737/3 "2019-04-01T11:10:32Z")

</div>

In Rails (elasticsearch-rails):

```
products = []
after = 0
loop do
  query = {
    size: query_size,
    query: {
      multi_match: {
        query: "black",
        fields: ["description", "title", "information", "params"]
      }
    },
    search_after: [after],
    sort: [{id: "asc"}]
  }
  results = Product.search(query).records
  results_amount = results.size
  products += results
  after += 10000

  break if results_amount == 0
end
```

---

<div class="post-metadata">

**Author:** ![unavailabl3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unavailabl3/32/59938_2.png) [@unavailabl3](https://discuss.elastic.co/u/unavailabl3)\
**Post date:** [April 1, 2019, 11:13am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737/5 "2019-04-01T11:13:20Z")

</div>

Thank you! **search\_after** documentation is what I was needed.

---

<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 29, 2019, 11:13am UTC](https://discuss.elastic.co/t/how-to-use-scroll-in-rails-ruby-to-get-more-than-10-000-records/174737/6 "2019-04-29T11:13:36Z")

</div>

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