# Is there a way to do scan with limit?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076>\
**Category:** Elasticsearch\
**Created:** [April 3, 2015, 12:31am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076 "2015-04-03T00:31:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chen\_Wang](https://avatars.discourse-cdn.com/v4/letter/c/b5a626/32.png) [@Chen\_Wang](https://discuss.elastic.co/u/Chen_Wang)\
**Post date:** [April 3, 2015, 12:31am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076/1 "2015-04-03T00:31:07Z")

</div>

I want to for example, fast get 1m out of 5m records.  
I am currently using:

SearchResponse scrollResp = this.client  
.prepareSearch(esQuery.indices)

.addFields(esQuery.fields)

.setSearchType(SearchType.SCAN)

.setScroll(

TimeValue

.timeValueSeconds(this.scrollTimeInSeconds))

.setQuery(esQuery.query)

.setSize(this.queryBatchSizePershard).execute().actionGet();

but the setSize defines how many records return per shard in one scroll. Is  
there a way to define a limit? or I have to control the limit in my code.

Chen

--  
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/5fb4751f-7080-423c-9acd-bfd56abfd844%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5fb4751f-7080-423c-9acd-bfd56abfd844%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![aaronmefford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaronmefford/32/460_2.png) [@aaronmefford](https://discuss.elastic.co/u/aaronmefford)\
**Post date:** [April 3, 2015, 4:12pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076/2 "2015-04-03T16:12:32Z")

</div>

Is there a reason not to control the limit in your code?

On Thursday, April 2, 2015 at 6:31:07 PM UTC-6, Chen Wang wrote:

> I want to for example, fast get 1m out of 5m records.  
> I am currently using:
> 
> SearchResponse scrollResp = this.client  
> .prepareSearch(esQuery.indices)
> 
> .addFields(esQuery.fields)
> 
> .setSearchType(SearchType.SCAN)
> 
> .setScroll(
> 
> TimeValue
> 
> .timeValueSeconds(this.scrollTimeInSeconds))
> 
> .setQuery(esQuery.query)
> 
> .setSize(this.queryBatchSizePershard).execute().actionGet();
> 
> but the setSize defines how many records return per shard in one scroll.  
> Is there a way to define a limit? or I have to control the limit in my code.
> 
> Chen

--  
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/b863b45e-5e1b-4aa7-8ac9-8b9715b7cc71%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b863b45e-5e1b-4aa7-8ac9-8b9715b7cc71%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Chen\_Wang](https://avatars.discourse-cdn.com/v4/letter/c/b5a626/32.png) [@Chen\_Wang](https://discuss.elastic.co/u/Chen_Wang)\
**Post date:** [April 3, 2015, 5:45pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076/3 "2015-04-03T17:45:07Z")

</div>

I was thinking that being able to do limit on ES side can reduce its query load.

--  
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/647b7cf9-1351-42d3-8238-fa2118b9f0b9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/647b7cf9-1351-42d3-8238-fa2118b9f0b9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![aaronmefford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaronmefford/32/460_2.png) [@aaronmefford](https://discuss.elastic.co/u/aaronmefford)\
**Post date:** [April 3, 2015, 8:25pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076/4 "2015-04-03T20:25:10Z")

</div>

Unless I am mistaken, that is already the job of the scan query, reducing  
the load caused by sorting a query. There would be no reduction by  
limiting the result set.

As I understand it the scan query identifies which shards has results, and  
then just starts serving the first results first without any sort. This  
continues until the shard has no more results. Elasticsearch only needs to  
track which results it has already sent, and sends the next batch,  
unsorted. This continues until you stop and the scan expires.

On Fri, Apr 3, 2015 at 11:45 AM, Chen Wang [chen.apache.solr@gmail.com](mailto:chen.apache.solr@gmail.com)  
wrote:

> I was thinking that being able to do limit on ES side can reduce its query  
> load.
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/-Y1wETKmuF0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/-Y1wETKmuF0/unsubscribe).  
> To unsubscribe from this group and all its topics, 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/647b7cf9-1351-42d3-8238-fa2118b9f0b9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/647b7cf9-1351-42d3-8238-fa2118b9f0b9%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CADqT7cH6w1EW%2B%2B%3Da%3D7C649dDReZOiCzB2X37hK%3DNiK%3D\_Fs0FHw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADqT7cH6w1EW%2B%2B%3Da%3D7C649dDReZOiCzB2X37hK%3DNiK%3D_Fs0FHw%40mail.gmail.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, 12:21am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-scan-with-limit/23076/5 "2017-07-06T00:21:45Z")

</div>


