# Combine 'should' with 'filter' (search API)

**URL:** https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129
**Category:** Elasticsearch
**Created:** [July 9, 2018, 10:02am UTC](https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129 "2018-07-09T10:02:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cweinberger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cweinberger/32/33147_2.png) [@cweinberger](https://discuss.elastic.co/u/cweinberger)
#### Post date: [July 9, 2018, 10:02am UTC](https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129/1 "2018-07-09T10:02:46Z")

</div>

When combining should with filter, the conditions in should are ignored. When using must, the filter works as expected.

**Query _without filter_:**

```auto
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "address": "mill"
              }
            },
            {
              "match": {
                "address": "lane"
              }
            }
          ]
        }
      }
    }

```

**Hits: 19**

**_Example including filter:_**

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "address": "mill"
          }
        },
        {
          "match": {
            "address": "lane"
          }
        }
      ],
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

```

**Hits: 217** (basically ignoring the _should_ condition)

Thanks for your help,  
Christian

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [July 10, 2018, 12:53pm UTC](https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129/2 "2018-07-10T12:53:04Z")

</div>

The `should` clause of the `bool` query is for _ **optional** _ search criteria. Let's say you are looking for hotels and would prefer for the hotel to have a swimming pool, but you would also like to see hotels without a swimming pool in the search results, then the query for swimming pool would be in the `should` clause. Elasticsearch will give the documents that match more `should` queries a higher score, so those documents will be ranked higher in the search results.

When you only have a `should` clause (as in your first query), then at least one of the `should` clauses must match for a document to be considered a hit.

When you combine a `should` clause with a `filter` than all `should` clauses are optional, so even documents that only match the filter will be returned. You can change that behavior by [setting `minimum_should_match`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html) on the `bool` query. You could set that for example to `1` to make it mandatory for at least one `should` clause to match. The docs have an example of that: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)

---

<div class="post-metadata">

### Author: ![cweinberger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cweinberger/32/33147_2.png) [@cweinberger](https://discuss.elastic.co/u/cweinberger)
#### Post date: [July 11, 2018, 9:07am UTC](https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129/3 "2018-07-11T09:07:58Z")

</div>

Great @abdon, Thanks a lot!

---

<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: [August 8, 2018, 9:07am UTC](https://discuss.elastic.co/t/combine-should-with-filter-search-api/139129/4 "2018-08-08T09:07:59Z")

</div>

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