# Top documents sorted by a particular field

**URL:** <https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484>\
**Category:** Elasticsearch\
**Created:** [January 24, 2012, 11:36pm UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484 "2012-01-24T23:36:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)\
**Post date:** [January 24, 2012, 11:36pm UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/1 "2012-01-24T23:36:17Z")

</div>

Hi,

I want to find the top N documents in my index sorted by a particular  
field, using a filter.

I find that I cannot combine a filter with a Match All query. The  
filter seems to have no effect, returning the top documents in the  
entire index every time regardless of the filter value.

Using the filter in conjunction with a query works.

What can I do?

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 25, 2012, 8:25am UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/2 "2012-01-25T08:25:05Z")

</div>

Have a look

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

> **[Bird’s Eye View on ElasticSearch its Query DSL](https://karussell.wordpress.com/2012/01/19/birds-eye-view-on-elasticsearch-its-query-dsl/)**
>
> I’ve copied the whole post into a gist so that you can simply clone, copy and paste the important stuff and even could contribute easily. Several times per month there pop up questions regard…

Peter.

On 25 Jan., 00:36, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:

> Hi,
> 
> I want to find the top N documents in my index sorted by a particular  
> field, using a filter.
> 
> I find that I cannot combine a filter with a Match All query. The  
> filter seems to have no effect, returning the top documents in the  
> entire index every time regardless of the filter value.
> 
> Using the filter in conjunction with a query works.
> 
> What can I do?

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 25, 2012, 8:25am UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/3 "2012-01-25T08:25:56Z")

</div>

BTW: Sorting != filter. The sorting is achieved with 'sort' and filter  
would restrict your search results ...

On 25 Jan., 00:36, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:

> Hi,
> 
> I want to find the top N documents in my index sorted by a particular  
> field, using a filter.
> 
> I find that I cannot combine a filter with a Match All query. The  
> filter seems to have no effect, returning the top documents in the  
> entire index every time regardless of the filter value.
> 
> Using the filter in conjunction with a query works.
> 
> What can I do?

---

<div class="post-metadata">

**Author:** ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)\
**Post date:** [January 25, 2012, 5:04pm UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/4 "2012-01-25T17:04:38Z")

</div>

Thanks, I seem to have gotten it to work.

On a related note -- is filtering possible on any field, or just  
indexed fields?

On Jan 25, 2:25 am, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> BTW: Sorting != filter. The sorting is achieved with 'sort' and filter  
> would restrict your search results ...
> 
> On 25 Jan., 00:36, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:
> 
> > Hi,
> 
> > I want to find the top N documents in my index sorted by a particular  
> > field, using a filter.
> 
> > I find that I cannot combine a filter with a Match All query. The  
> > filter seems to have no effect, returning the top documents in the  
> > entire index every time regardless of the filter value.
> 
> > Using the filter in conjunction with a query works.
> 
> > What can I do?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 25, 2012, 9:47pm UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/5 "2012-01-25T21:47:04Z")

</div>

Just indexed fields mainly, though you can have a script filter with a script that uses \_source to load and parse the source to get a value and filter by it (which is expensive).

On Wednesday, January 25, 2012 at 7:04 PM, Frank LaRosa wrote:

> Thanks, I seem to have gotten it to work.
> 
> On a related note -- is filtering possible on any field, or just  
> indexed fields?
> 
> On Jan 25, 2:25 am, Karussell \<[tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) ([http://googlemail.com](http://googlemail.com))\> wrote:
> 
> > BTW: Sorting != filter. The sorting is achieved with 'sort' and filter  
> > would restrict your search results ...
> > 
> > On 25 Jan., 00:36, Frank LaRosa \<[fr...@studyblue.com](mailto:fr...@studyblue.com) ([http://studyblue.com](http://studyblue.com))\> wrote:
> > 
> > > Hi,
> > 
> > > I want to find the top N documents in my index sorted by a particular  
> > > field, using a filter.
> > 
> > > I find that I cannot combine a filter with a Match All query. The  
> > > filter seems to have no effect, returning the top documents in the  
> > > entire index every time regardless of the filter value.
> > 
> > > Using the filter in conjunction with a query works.
> > 
> > > What can I do?

---

<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:41am UTC](https://discuss.elastic.co/t/top-documents-sorted-by-a-particular-field/6484/6 "2017-07-06T03:41:29Z")

</div>


