# Detecting end of Scroll

**URL:** https://discuss.elastic.co/t/detecting-end-of-scroll/18134
**Category:** Elasticsearch
**Created:** [June 16, 2014, 2:45pm UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134 "2014-06-16T14:45:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)
#### Post date: [June 16, 2014, 2:45pm UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134/1 "2014-06-16T14:45:00Z")

</div>

According to the documentation, you know when you have reached the end of  
the scroll results when you receive zero hits in the response.  
However, if your SearchType is SCAN, then you receive zero hits in the  
_first_ result,  
The question is how to avoid this ambiguity.

My code that handles the scrolling results doesn't know what the SearchType  
is - so it doesn't know how to differentiate between the two scenarios of  
in which we receive zero hits.

Can the end of the results not be signalled through the scrollId?  
I kind of expected the scrollId to be null when we reached the end of the  
results (but I can see its not).

That said, I do notice that when the end of the results are reached, the  
scrollId stops changing - and its also a lot shorter - and also it seems to  
always be the same value (c2NhbjswOzE7dG90YWxfaGl0czoxOw==)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6a9da841-1c70-4179-a0ba-b6f145363e23%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a9da841-1c70-4179-a0ba-b6f145363e23%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ralph\_Meijer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ralph_meijer/32/1483_2.png) [@Ralph\_Meijer](https://discuss.elastic.co/u/Ralph_Meijer)
#### Post date: [June 16, 2014, 2:58pm UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134/2 "2014-06-16T14:58:04Z")

</div>

On Mon, Jun 16, 2014 at 07:45:00AM -0700, mooky wrote:

> [..]  
> That said, I do notice that when the end of the results are reached, the  
> scrollId stops changing - and its also a lot shorter - and also it seems to  
> always be the same value (c2NhbjswOzE7dG90YWxfaGl0czoxOw==)

That scroll id is the base64 encoding of the following string:

scan;0;1;total\_hits:1;

--  
Groetjes,

ralphm

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/20140616145804.GB25839%40ik.nu](https://groups.google.com/d/msgid/elasticsearch/20140616145804.GB25839%40ik.nu).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)
#### Post date: [June 16, 2014, 3:15pm UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134/3 "2014-06-16T15:15:55Z")

</div>

Oh, I see.  
Can we make any assumption about the scrollId not changing when we reach  
the end of the results?

On Monday, 16 June 2014 15:58:20 UTC+1, Ralph Meijer wrote:

> On Mon, Jun 16, 2014 at 07:45:00AM -0700, mooky wrote:
> 
> > [..]  
> > That said, I do notice that when the end of the results are reached, the  
> > scrollId stops changing - and its also a lot shorter - and also it seems  
> > to  
> > always be the same value (c2NhbjswOzE7dG90YWxfaGl0czoxOw==)
> 
> That scroll id is the base64 encoding of the following string:
> 
> scan;0;1;total\_hits:1;
> 
> --  
> Groetjes,
> 
> ralphm

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/34e73a88-285e-4ea7-8468-5dc11b7ee9bd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/34e73a88-285e-4ea7-8468-5dc11b7ee9bd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![mooky](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mooky](https://discuss.elastic.co/u/mooky)
#### Post date: [June 16, 2014, 4:59pm UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134/4 "2014-06-16T16:59:06Z")

</div>

Just to confirm:  
Is it correct that in order to automatically clean up the resources  
associated with a scroll, you must execute it until zero results are  
retrieved?  
What about the case when all of the hits are returned in the first result  
(ie num hits = total hits)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/bb66ee75-c40b-465e-96c7-6433c4c08a58%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bb66ee75-c40b-465e-96c7-6433c4c08a58%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:22am UTC](https://discuss.elastic.co/t/detecting-end-of-scroll/18134/5 "2017-07-06T01:22:04Z")

</div>


