# Caching a search query for pagination

**URL:** <https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931>\
**Category:** Elasticsearch\
**Created:** [May 13, 2013, 8:35am UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931 "2013-05-13T08:35:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eran\_Eidinger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eran_eidinger/32/44831_2.png) [@Eran\_Eidinger](https://discuss.elastic.co/u/Eran_Eidinger)\
**Post date:** [May 13, 2013, 8:35am UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931/1 "2013-05-13T08:35:09Z")

</div>

When paginating through a query using "from" and "size", does the query stay cached, or is it evaluated anew each time on the server?

If not, is there a way to "freeze" a query to paginate it?  
example:

curl -XPOST '[http://localhost:9200/index1/type1/\_search](http://localhost:9200/index1/type1/_search)' -d '{  
"query":{"match\_all":{}},  
"sort":{  
"\_script" :{"script" : "Math.random()",  
"type" : "number",  
"order" : "asc"}},  
"from":0,"size":10}'

Next time i run this, say with "from":10,"size":10, the ordering will be created anew, right? so i might even get duplicates between the two calls?

This seems an awful waste of CPU if all I want is to paginate through a query.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 14, 2013, 12:55am UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931/2 "2013-05-14T00:55:41Z")

</div>

Look into the scroll feature, which might be a better solution for your use  
case: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/scroll/)

But to answer your question, the query is not cached and will be executed.  
Queries are never cached, even if the parameters remain the same. Filters  
can be cached.

--  
Ivan

On Mon, May 13, 2013 at 1:35 AM, eranid [eranid@gmail.com](mailto:eranid@gmail.com) wrote:

> When paginating through a query using "from" and "size", does the query  
> stay  
> cached, or is it evaluated anew each time on the server?
> 
> If not, is there a way to "freeze" a query to paginate it?  
> example:
> 
> curl -XPOST '[http://localhost:9200/index1/type1/\_search](http://localhost:9200/index1/type1/_search)' -d '{  
> "query":{"match\_all":{}},  
> "sort":{  
> "\_script" :{"script" : "Math.random()",  
> "type" : "number",  
> "order" : "asc"}},  
> "from":0,"size":10}'
> 
> Next time i run this, say with "from":10,"size":10, the ordering will be  
> created anew, right? so i might even get duplicates between the two calls?
> 
> This seems an awful waste of CPU if all I want is to paginate through a  
> query.
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Caching-a-search-query-for-pagination-tp4034705.html](http://elasticsearch-users.115913.n3.nabble.com/Caching-a-search-query-for-pagination-tp4034705.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Eran\_Eidinger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eran_eidinger/32/44831_2.png) [@Eran\_Eidinger](https://discuss.elastic.co/u/Eran_Eidinger)\
**Post date:** [May 14, 2013, 2:39pm UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931/3 "2013-05-14T14:39:37Z")

</div>

That's really great! Thanks alot.

Regarding caching queries, I saw that you can set the \_cache when using a filter, [http://www.elasticsearch.org/guide/reference/query-dsl/query-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter/), but I do not see how I can later use that cached filter in another query?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 14, 2013, 11:38pm UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931/4 "2013-05-14T23:38:44Z")

</div>

The filter cache is applied behind the scenes, out of the developers  
control.

--  
Ivan

On Tue, May 14, 2013 at 7:39 AM, eranid [eranid@gmail.com](mailto:eranid@gmail.com) wrote:

> That's really great! Thanks alot.
> 
> Regarding caching queries, I saw that you can set the \_cache when using a  
> filter,  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter/), but  
> I  
> do not see how I can later use that cached filter in another query?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Caching-a-search-query-for-pagination-tp4034705p4034797.html](http://elasticsearch-users.115913.n3.nabble.com/Caching-a-search-query-for-pagination-tp4034705p4034797.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:36am UTC](https://discuss.elastic.co/t/caching-a-search-query-for-pagination/11931/5 "2017-07-06T02:36:44Z")

</div>


