# How filters mechanism works

**URL:** <https://discuss.elastic.co/t/how-filters-mechanism-works/1382>\
**Category:** Elasticsearch\
**Created:** [May 27, 2015, 11:47am UTC](https://discuss.elastic.co/t/how-filters-mechanism-works/1382 "2015-05-27T11:47:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisMark](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrismark/32/400_2.png) [@ChrisMark](https://discuss.elastic.co/u/ChrisMark)\
**Post date:** [May 27, 2015, 11:47am UTC](https://discuss.elastic.co/t/how-filters-mechanism-works/1382/1 "2015-05-27T11:47:59Z")

</div>

Hello,

i was wondering how elasticsearch implements the filtering process.  
For example if do a text-search for the word "like" and then i add a filter "type:tweet", how this operation will be executed?  
I imagine that elastiseach first searchs in inverted index for the word "like", finds the docs, and then filters them returning the final bucket.  
Is that right, or i'm mising sth?

---

<div class="post-metadata">

**Author:** ![Joshua\_P](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@Joshua\_P](https://discuss.elastic.co/u/Joshua_P)\
**Post date:** [May 27, 2015, 1:40pm UTC](https://discuss.elastic.co/t/how-filters-mechanism-works/1382/2 "2015-05-27T13:40:12Z")

</div>

From the [documentation](https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html#_internal_filter_operation):

> Internal Filter Operation

> Internally, Elasticsearch is performing several operations when executing a filter:

> Find matching docs.

> The term filter looks up the term XHDK-A-1293-#fJ3 in the inverted index and retrieves the list of documents that contain that term. In this case, only document 1 has the term we are looking for.

> Build a bitset.

> The filter then builds a bitset--an array of 1s and 0s—that describes which documents contain the term. Matching documents receive a 1 bit. In our example, the bitset would be [1,0,0,0].

> Cache the bitset.

> Last, the bitset is stored in memory, since we can use this in the future and skip steps 1 and 2. This adds a lot of performance and makes filters very fast.

> When executing a filtered query, the filter is executed before the query. The resulting bitset is given to the query, which uses it to simply skip over any documents that have already been excluded by the filter. This is one of the ways that filters can improve performance. Fewer documents evaluated by the query means faster response times.

---

<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:11am UTC](https://discuss.elastic.co/t/how-filters-mechanism-works/1382/3 "2017-07-06T00:11:38Z")

</div>


