# Can a Filter be Applied with "OR" Logic?

**URL:** https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959
**Category:** Elasticsearch
**Created:** [March 11, 2021, 2:52pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959 "2021-03-11T14:52:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![spyderman4g63](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spyderman4g63/32/64172_2.png) [@spyderman4g63](https://discuss.elastic.co/u/spyderman4g63)
#### Post date: [March 11, 2021, 2:52pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/1 "2021-03-11T14:52:41Z")

</div>

I'm trying to mimic the functionality of another search engine. The functionality I want is that when a user selects a filter it first limits a search, as expected. However, I want to still return all possible aggregation values for the entire data set (not just the new filtered data set). When the user selects a subsequent filter it expands the number of results.

An Example of this functionality are Amazon's refinements:

 ![Screen Shot 2021-03-11 at 9.49.47 AM](https://us1.discourse-cdn.com/elastic/original/3X/1/5/1591043c3b1689a51f91c6bf35809be0ec7d9b7f.png)

When I select "Acer" it limits the results to only Acer. When I then select "HP" it expands the results to include Acer or HP.

I'm trying to utilize filters vs query because I don't want to affect relevance. Any ideas?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 11, 2021, 3:06pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/2 "2021-03-11T15:06:29Z")

</div>

Hi John. Check out [post filters](https://www.elastic.co/guide/en/elasticsearch/reference/7.11/filter-search-results.html#post-filter)

---

<div class="post-metadata">

### Author: ![spyderman4g63](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spyderman4g63/32/64172_2.png) [@spyderman4g63](https://discuss.elastic.co/u/spyderman4g63)
#### Post date: [March 17, 2021, 2:37pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/3 "2021-03-17T14:37:45Z")

</div>

Thanks. Post\_filer might be what I want, but I can't seem to find any doc on using multiple `post_filter` conditions or passing an array to `post_filter`. Can you apply multiple `post_filter`s?

I am hoping for something like:

```auto
"post_filter": [
    "term":{"color":"red"},
    "term":{"color":"blue"}
]

```

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 17, 2021, 2:43pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/4 "2021-03-17T14:43:46Z")

</div>

[bool](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) queries are the way to combine multiple query clauses using Boolean logic. An array of `should` clauses would be the way to OR clauses.

If you only have `term` clauses for the same field a shorter way of doing this is to just use a [terms](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html) query.

---

<div class="post-metadata">

### Author: ![spyderman4g63](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spyderman4g63/32/64172_2.png) [@spyderman4g63](https://discuss.elastic.co/u/spyderman4g63)
#### Post date: [March 23, 2021, 2:37pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/5 "2021-03-23T14:37:52Z")

</div>

Thanks again. I ended up with something like:

```
{
  "post_filter":{
      "bool":{
          "should":{
              {"term":{"color":"red"}},
              {"term":{"color":"blue"}}
          }
      }
  }
}

```

This has the effect I wanted for the color filter. I think I may need a mix of `post_fitter` and `filter` when combining multiple filters like "color" and "size".

---

<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: [April 20, 2021, 2:38pm UTC](https://discuss.elastic.co/t/can-a-filter-be-applied-with-or-logic/266959/6 "2021-04-20T14:38:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
