# How to sort and then apply a limit filter

**URL:** <https://discuss.elastic.co/t/how-to-sort-and-then-apply-a-limit-filter/9245>\
**Category:** Elasticsearch\
**Created:** [October 4, 2012, 8:10pm UTC](https://discuss.elastic.co/t/how-to-sort-and-then-apply-a-limit-filter/9245 "2012-10-04T20:10:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bragboy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bragboy/32/23058_2.png) [@bragboy](https://discuss.elastic.co/u/bragboy)\
**Post date:** [October 4, 2012, 8:10pm UTC](https://discuss.elastic.co/t/how-to-sort-and-then-apply-a-limit-filter/9245/1 "2012-10-04T20:10:49Z")

</div>

Hi,

I want to sort the results according to a specific field AND THEN apply the  
limit filter.

A simple example would be to sort a user model by full\_name and then apply  
the limit say top 100. In this case, however there is no query - get all  
the values, hence no score.

However, this is not happening in Elastic search. It first applies the  
limit AND THEN sorts which is giving me undesired results. Can anyone point  
me what I am doing wrong ?

{  
"sort": [  
"full\_name"  
],  
"filter": {  
"limit": {  
"value": 100  
}  
}  
}

Thanks,  
Bragboy

--

---

<div class="post-metadata">

**Author:** ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)\
**Post date:** [October 6, 2012, 6:02am UTC](https://discuss.elastic.co/t/how-to-sort-and-then-apply-a-limit-filter/9245/2 "2012-10-06T06:02:32Z")

</div>

Hi,  
Unless I'm confused, I think you're simply looking for the size parameter:

> **[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.

Combined with a match all query and your existing sort:

> **[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.

The limit filter is somewhat special case behavior that grabs a sampling of  
docs and then applies the search.

Best Regards,  
Paul

On Thursday, October 4, 2012 2:10:49 PM UTC-6, bragboy wrote:

> Hi,
> 
> I want to sort the results according to a specific field AND THEN apply  
> the limit filter.
> 
> A simple example would be to sort a user model by full\_name and then apply  
> the limit say top 100. In this case, however there is no query - get all  
> the values, hence no score.
> 
> However, this is not happening in Elastic search. It first applies the  
> limit AND THEN sorts which is giving me undesired results. Can anyone point  
> me what I am doing wrong ?
> 
> {  
> "sort": [  
> "full\_name"  
> ],  
> "filter": {  
> "limit": {  
> "value": 100  
> }  
> }  
> }
> 
> Thanks,  
> Bragboy

--

---

<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, 3:09am UTC](https://discuss.elastic.co/t/how-to-sort-and-then-apply-a-limit-filter/9245/3 "2017-07-06T03:09:54Z")

</div>


