# Term vs terms filter

**URL:** <https://discuss.elastic.co/t/term-vs-terms-filter/15533>\
**Category:** Elasticsearch\
**Created:** [January 31, 2014, 5:15pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533 "2014-01-31T17:15:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Johnson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_johnson/32/1196_2.png) [@Matthias\_Johnson](https://discuss.elastic.co/u/Matthias_Johnson)\
**Post date:** [January 31, 2014, 5:15pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/1 "2014-01-31T17:15:35Z")

</div>

I'm filtering on terms. it seems that there are 2 choices: term filter[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html)and terms  
filter[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html)  
.

In my case I have a list of terms displayed, from which the user can select  
to be filtered on. This works very well with the terms filter, since it  
have an array. Additionally, the terms filter does just fine with a single  
value in the array.

The terms filter also offers much in terms of flexibility for tweaking the  
execution mode and caching, which the term filter does not.

That makes me wonder about the term filter and where it should be used.

Are there any usage guidelines for one over the other? Seems that passing  
an array to terms would not be a lot different over building an array of  
term filters inside an _or_ filter.

@matthias

--  
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/7336d8cc-0a6b-4094-969a-61c8de3bb2f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7336d8cc-0a6b-4094-969a-61c8de3bb2f1%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 31, 2014, 5:34pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/2 "2014-01-31T17:34:50Z")

</div>

Mathias, you're correct in your observations. The only thing you might want  
to be aware of is the terms filter is automatically cached, whereas an or  
filter of many term filters is not automatically cached. But the results  
you will get back should be the same.

--  
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/4944f3bc-0ee5-4d07-ac27-801af53ed970%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4944f3bc-0ee5-4d07-ac27-801af53ed970%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Matthias\_Johnson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthias_johnson/32/1196_2.png) [@Matthias\_Johnson](https://discuss.elastic.co/u/Matthias_Johnson)\
**Post date:** [January 31, 2014, 5:48pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/3 "2014-01-31T17:48:31Z")

</div>

Thanks Binh. Out of curiosity do you know of performance differences  
between the 2 choices? it would seem that the terms filter with it's  
caching may provide better performance?

@matthias

On Friday, January 31, 2014 10:34:50 AM UTC-7, Binh Ly wrote:

> Mathias, you're correct in your observations. The only thing you might  
> want to be aware of is the terms filter is automatically cached, whereas an  
> or filter of many term filters is not automatically cached. But the results  
> you will get back should be the same.

--  
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/f2d6b901-d415-45fa-8687-4fb91a6d961b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f2d6b901-d415-45fa-8687-4fb91a6d961b%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 31, 2014, 5:54pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/4 "2014-01-31T17:54:35Z")

</div>

Matthias,

For the terms filter, let's say you do:

{  
"query": {  
"filtered": {  
"filter": {  
"terms": {  
"a": [  
"a",  
"b"  
]  
}  
}  
}  
}  
}

If you run the above exact query many times, the succeeding calls will be  
faster since the first call will cache the result of the above filter.

The or filter is not cached by default (but you can cache it explicitly if  
needed) so if you run an or filter many times, it will re-evaluate for each  
succeeding call (unless you explicitly cache it).

This might help also:

> **[All About Elasticsearch Filter BitSets
	  	 | Elastic](https://www.elastic.co/blog/all-about-elasticsearch-filter-bitsets)**
>
> WARNING: This article contains outdated information. We no longer recommend taking its advice. When building queries in Elasticsearch, you’ll often find yourself composing sets of filters. Say you nee...

--  
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/8a112f6b-d310-44f8-84a5-c9df65e6a9f1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a112f6b-d310-44f8-84a5-c9df65e6a9f1%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![anloya](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@anloya](https://discuss.elastic.co/u/anloya)\
**Post date:** [September 9, 2015, 3:10pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/5 "2015-09-09T15:10:44Z")

</div>

As discussed here as well as what is stated in the documentation, the only difference is "caching".  
So does this mean that if I cache the OR filter on a big list (30+) of individual term filters, it will not have **any performance drawback** compared to terms filter with these 30+ values?

---

<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 5, 2017, 11:51pm UTC](https://discuss.elastic.co/t/term-vs-terms-filter/15533/6 "2017-07-05T23:51:32Z")

</div>


