# Scrolling / sorting

**URL:** <https://discuss.elastic.co/t/scrolling-sorting/4907>\
**Category:** Elasticsearch\
**Created:** [July 20, 2011, 12:27pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907 "2011-07-20T12:27:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Kriesten](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jan\_Kriesten](https://discuss.elastic.co/u/Jan_Kriesten)\
**Post date:** [July 20, 2011, 12:27pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/1 "2011-07-20T12:27:41Z")

</div>

Hi,

I've got the following code to build up a scrolling search with  
elasticsearch:

public ActionFuture execute(final Client client) {  
final SearchRequest request = Requests.searchRequest();  
request.searchType(SearchType.SCAN);  
request.scroll(new TimeValue(timeout));  
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();  
sourceBuilder.query(query);  
sourceBuilder.from(0);  
sourceBuilder.size(scrollSize);  
sourceBuilder.explain(true);  
sourceBuilder.sort(sortField, SortOrder.ASC);  
request.source(sourceBuilder);

return client.search(request);  
}

There are two problems I encounter:

First, instead of returning 'scrollSize' elements I always get  
'scrollSize \* 3' elements with each scroll request. Did I miss  
something here?

Second, sorting is completely ignored with this query. What am I doing  
wrong?

Thanks for any hints!

Best regards, --- Jan.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 20, 2011, 12:36pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/2 "2011-07-20T12:36:03Z")

</div>

Hi Jan

> First, instead of returning 'scrollSize' elements I always get  
> 'scrollSize \* 3' elements with each scroll request. Did I miss  
> something here?
> 
> Second, sorting is completely ignored with this query. What am I doing  
> wrong?

Nothing - it is working exactly as expected. The scan search\_type is  
intended for pulling large numbers of docs out of ES efficiently. It is  
not intended for returning search results to users.

Each scroll request receives a maximum of scrollSize \* $no\_of\_shards  
results (until each shard runs out of more results).

And sorting is ignored for scan requests.

clint

---

<div class="post-metadata">

**Author:** ![Jan\_Kriesten](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jan\_Kriesten](https://discuss.elastic.co/u/Jan_Kriesten)\
**Post date:** [July 20, 2011, 2:09pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/3 "2011-07-20T14:09:56Z")

</div>

Hi Clint,

> Each scroll request receives a maximum of scrollSize \* $no\_of\_shards  
> results (until each shard runs out of more results).

is that the expected behavior for SearchSourceBuilder as well when  
setting from/size? Cause setting size to 10 also results in 30 hits.

Best regards, --- Jan.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 20, 2011, 2:20pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/4 "2011-07-20T14:20:01Z")

</div>

Hi Jan

> > Each scroll request receives a maximum of scrollSize \* $no\_of\_shards  
> > results (until each shard runs out of more results).
> 
> is that the expected behavior for SearchSourceBuilder as well when  
> setting from/size? Cause setting size to 10 also results in 30 hits.

I can only speak for the REST interface, don't know the Java API.

But for search\_type = scan, you will always get back a maximum of  
size x shards results on each scroll request. (size is, by default, 10)

So if you had 5 shards, you would get back a max of 50 results at a  
time, until each shard starts running out of results.

You know that you have pulled all results when you get zero results back

clint

---

<div class="post-metadata">

**Author:** ![Jan\_Kriesten](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jan\_Kriesten](https://discuss.elastic.co/u/Jan_Kriesten)\
**Post date:** [July 20, 2011, 2:24pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/5 "2011-07-20T14:24:31Z")

</div>

Hi Clinton,

> But for search\_type = scan, you will always get back a maximum of  
> size x shards results on each scroll request. (size is, by default, 10)

I meant SearchType.DFS\_QUERY\_AND\_FETCH is also returning size x shards -  
is that expected?

Best regards, --- Jan.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 20, 2011, 2:30pm UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/6 "2011-07-20T14:30:16Z")

</div>

Hi Jan

> > But for search\_type = scan, you will always get back a maximum of  
> > size x shards results on each scroll request. (size is, by default, 10)
> 
> I meant SearchType.DFS\_QUERY\_AND\_FETCH is also returning size x shards -  
> is that expected?

Ah right. Then yes, that is expected:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

---

<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, 4:00am UTC](https://discuss.elastic.co/t/scrolling-sorting/4907/7 "2017-07-06T04:00:03Z")

</div>


