# Range filter in should match query

**URL:** <https://discuss.elastic.co/t/range-filter-in-should-match-query/311435>\
**Category:** Elasticsearch\
**Created:** [August 4, 2022, 2:00pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435 "2022-08-04T14:00:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [August 4, 2022, 2:00pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435/1 "2022-08-04T14:00:00Z")

</div>

Hi, let's say I have 3 documents,

```auto
id=1, "text" = "my name is john", "date" = "2021"
id=2, "text" = "my name is alice", "date" = "2017"
id=3, "text" = "my name is steven", "date" = "2019"

```

I want to find all documents that have one of the exact phrases (e.g. search for all documents that have **john** OR **alice** ). Used match\_phrase query, and it works ok If I don't put range filter on the date field. It works though if I run the **post\_filter** query, but since it's not really recommended cause of performance (aggregations). Is there any "smarter" way of doing this (give me all documents containing one or another phrase in the date range provided)?

Query

```auto
{
  "from": 0,
  "size": 10000,
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "text": "john"
          }
        },
        {
          "match_phrase": {
            "text": "steven"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ],
  "post_filter": {
    "range": {
      "date": {
        "gte": "2021",
        "lte": "2022"
      }
    }
  }
} 

```

This returns only **John** because it finds **John and Steven** in the first block and then filters out by date only **John** which is good. Tried to put **filter range** in bool must and in should , but it returns both documents. Any ideas or I can only use post\_filter in my case?

Thnx

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [August 4, 2022, 2:37pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435/2 "2022-08-04T14:37:26Z")

</div>

Hi @ansamHox

You can use "filter" for range date and should for match. Like this:  
When you use "filter", you will have the benefit of the cache.

```auto
POST test/_bulk
{"index":{}}
{"text":"my name is john","date":2021}
{"index":{}}
{"text":"my name is alice","date":2017}
{"index":{}}
{"text":"my name is steven","date":2019}

GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": 2021,
              "lte": 2022
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "text": "john"
          }
        },
        {
          "match": {
            "text": "alice"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [August 5, 2022, 1:23pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435/3 "2022-08-05T13:23:43Z")

</div>

In my case this doesn't work, it brings back over 10k results, but without a filter, it brings back only 700 results. And I even put a filter from 2000 to 2022 (that's all documents basically). With post\_filter it works, it's also 700 results, that's why I was wondering if there is any other approach. Also, when I try to search in the results for one or another string, for one there are around 300 documents, and for another are around 500, and some of them have both entries so that makes sense that there are 700 hits.

I didn't mention that I have encrypted values with my custom analyzer, but that's why I use match\_phrase case 1 "real letter" is encoded in 16 characters, etc.

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [August 5, 2022, 1:41pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435/4 "2022-08-05T13:41:14Z")

</div>

Here's a solution if anyone is having same issue:

Combining `should` and `filter` is problematic without using `minimum_should_match` - [Boolean query | Elasticsearch Guide [8.3] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#bool-min-should-match) ( If the `bool` query includes at least one `should` clause and no `must` or `filter` clauses, the default value is `1`. Otherwise, the default value is `0`. )

If I put `"minimum_should_match ": 1` , then it works as expected

---

<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:** [September 2, 2022, 1:41pm UTC](https://discuss.elastic.co/t/range-filter-in-should-match-query/311435/5 "2022-09-02T13:41:32Z")

</div>

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