# termQuery vs. termFilter

**URL:** <https://discuss.elastic.co/t/termquery-vs-termfilter/4839>\
**Category:** Elasticsearch\
**Created:** [July 12, 2011, 1:44pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839 "2011-07-12T13:44:32Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [July 12, 2011, 1:44pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/1 "2011-07-12T13:44:32Z")

</div>

Which query should I prefer? This filter query:

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
FilterBuilders.termFilter("someField", "someValue")));

or this termQuery:

srb.setQuery(QueryBuilders.termQuery("someField", "someValue"));

Regarding performance and memory? Or is there even a better way of  
doing this?

Regards,  
Peter.

---

<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 12, 2011, 1:50pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/2 "2011-07-12T13:50:19Z")

</div>

On Tue, 2011-07-12 at 06:44 -0700, Karussell wrote:

> Which query should I prefer? This filter query:

Use a term query if the terms you are looking for should affect the  
\_score (relevance). If it should just exclude all docs except for those  
that match, use a term filter.

clint

---

<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:** [July 12, 2011, 2:16pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/3 "2011-07-12T14:16:27Z")

</div>

Ok, I do not need the score. So, can I even go with scan search + the  
termFilter or which one would be the fastest?

I'm expecting only one or two results ...

On 12 Jul., 15:50, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> On Tue, 2011-07-12 at 06:44 -0700, Karussell wrote:
> 
> > Which query should I prefer? This filter query:
> 
> Use a term query if the terms you are looking for should affect the  
> \_score (relevance). If it should just exclude all docs except for those  
> that match, use a term filter.
> 
> clint

---

<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:** [July 12, 2011, 5:09pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/4 "2011-07-12T17:09:57Z")

</div>

If its a term value that is easily cacheable (has a "small" amount of values, and its going to be repeated), then a term filter is the way to go usually. If not, a term query.

On Tuesday, July 12, 2011 at 5:16 PM, Karussell wrote:

> Ok, I do not need the score. So, can I even go with scan search + the  
> termFilter or which one would be the fastest?
> 
> I'm expecting only one or two results ...
> 
> On 12 Jul., 15:50, Clinton Gormley \<[clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) ([http://iannounce.co.uk](http://iannounce.co.uk))\> wrote:
> 
> > On Tue, 2011-07-12 at 06:44 -0700, Karussell wrote:
> > 
> > > Which query should I prefer? This filter query:
> > 
> > Use a term query if the terms you are looking for should affect the  
> > \_score (relevance). If it should just exclude all docs except for those  
> > that match, use a term filter.
> > 
> > clint

---

<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:** [July 12, 2011, 5:17pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/5 "2011-07-12T17:17:12Z")

</div>

ok. thanks. and do you think that a score-less search could improve  
performance or is it neglectable for only a few docs for each value of  
that field?

Regards,  
Peter.

On 12 Jul., 19:09, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> If its a term value that is easily cacheable (has a "small" amount of values, and its going to be repeated), then a term filter is the way to go usually. If not, a term query.
> 
> On Tuesday, July 12, 2011 at 5:16 PM, Karussell wrote:
> 
> > Ok, I do not need the score. So, can I even go with scan search + the  
> > termFilter or which one would be the fastest?
> 
> > I'm expecting only one or two results ...
> 
> > On 12 Jul., 15:50, Clinton Gormley \<[clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) ([http://iannounce.co.uk](http://iannounce.co.uk))\> wrote:
> > 
> > > On Tue, 2011-07-12 at 06:44 -0700, Karussell wrote:
> > > 
> > > > Which query should I prefer? This filter query:
> 
> > > Use a term query if the terms you are looking for should affect the  
> > > \_score (relevance). If it should just exclude all docs except for those  
> > > that match, use a term filter.
> 
> > > clint

---

<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:** [July 12, 2011, 5:40pm UTC](https://discuss.elastic.co/t/termquery-vs-termfilter/4839/6 "2011-07-12T17:40:37Z")

</div>

Negligible, and use query\_and\_fetch search\_type if you want all.

On Tuesday, July 12, 2011 at 8:17 PM, Karussell wrote:

> ok. thanks. and do you think that a score-less search could improve  
> performance or is it neglectable for only a few docs for each value of  
> that field?
> 
> Regards,  
> Peter.
> 
> On 12 Jul., 19:09, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> 
> > If its a term value that is easily cacheable (has a "small" amount of values, and its going to be repeated), then a term filter is the way to go usually. If not, a term query.
> > 
> > On Tuesday, July 12, 2011 at 5:16 PM, Karussell wrote:
> > 
> > > Ok, I do not need the score. So, can I even go with scan search + the  
> > > termFilter or which one would be the fastest?
> > 
> > > I'm expecting only one or two results ...
> > 
> > > On 12 Jul., 15:50, Clinton Gormley \<[clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) ([http://iannounce.co.uk](http://iannounce.co.uk))\> wrote:
> > > 
> > > > On Tue, 2011-07-12 at 06:44 -0700, Karussell wrote:
> > > > 
> > > > > Which query should I prefer? This filter query:
> > 
> > > > Use a term query if the terms you are looking for should affect the  
> > > > \_score (relevance). If it should just exclude all docs except for those  
> > > > that match, use a term filter.
> > 
> > > > 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/termquery-vs-termfilter/4839/7 "2017-07-06T04:00:56Z")

</div>


