# Creating Query that matches values using OR within a specific timeframe

**URL:** <https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506>\
**Category:** Elasticsearch\
**Created:** [April 25, 2019, 4:25pm UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506 "2019-04-25T16:25:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lelwar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lelwar/32/45040_2.png) [@lelwar](https://discuss.elastic.co/u/lelwar)\
**Post date:** [April 25, 2019, 4:25pm UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506/1 "2019-04-25T16:25:37Z")

</div>

Hello!  
I am trying to create a query to search for different values of a specific field within a timeframe. For example, I want to search for a response with the value “123” OR “321” , AND it was created in the last 5 minutes:

GET /index1/\_search  
{  
"query": {  
"bool": {  
"must": {  
"bool" : {  
"should": [  
{  
"match": {  
"responseCode.keyword": "123"  
}  
},  
{  
"match": {  
"responseCode.keyword": "321"  
}  
}  
],  
"must": [  
{  
"range": {  
"ltimestamp": {  
"from": "now-5m",  
"to": "now"  
}  
}  
}  
]  
}  
}  
}  
}  
}

Nonetheless, when I execute this query I get values that do not correspond to those response codes. What am I missing?

regards,

Israel

---

<div class="post-metadata">

**Author:** ![lelwar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lelwar/32/45040_2.png) [@lelwar](https://discuss.elastic.co/u/lelwar)\
**Post date:** [April 25, 2019, 4:27pm UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506/2 "2019-04-25T16:27:42Z")

</div>

Sorry, I try to format this now:

```
GET /index1/_search
{
  "query": {
    "bool": {
      "must": {
        "bool" : { 
          "should": [
        {
          "match": {
            "responseCode.keyword": "123"
          }
        },
        {
          "match": {
            "responseCode.keyword": "321"
          }
        }
      ],
          "must": [
        {
          "range": {
             "log_timestamp": {
                "from": "now-5m",
                "to": "now"
              }
           }
        }
      ] 
        }
      }
    }
  }
}
```

---

<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:** [April 26, 2019, 7:44am UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506/3 "2019-04-26T07:44:10Z")

</div>

If you combine a `should` clause with a `must` clause then any query inside of the `should` clause is optional. In other words, if a document just matches the query in the `must` clause, it's also going to be a hit (but with a lower score).

What you want to achieve can be done by making the should clause it's own `bool` query, and putting that `bool` query inside the `must` clause:

```auto
GET /index1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "log_timestamp": {
              "from": "now-5m",
              "to": "now"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "responseCode.keyword": "123"
                }
              },
              {
                "match": {
                  "responseCode.keyword": "321"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Now, at least one of the queries in the `should` clause has to match for a document to be a hit.

---

<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:** [April 26, 2019, 8:00am UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506/4 "2019-04-26T08:00:26Z")

</div>

Or, alternatively, you can add `minimum_should_match` to the query. That parameter allows you to specify how many of the queries in the `should` clause have to match for a document to be considered a hit:

```auto
GET /index1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "responseCode.keyword": "123"
          }
        },
        {
          "match": {
            "responseCode.keyword": "321"
          }
        }
      ],
      "must": [
        {
          "range": {
            "log_timestamp": {
              "from": "now-5m",
              "to": "now"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

---

<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:** [May 24, 2019, 8:00am UTC](https://discuss.elastic.co/t/creating-query-that-matches-values-using-or-within-a-specific-timeframe/178506/5 "2019-05-24T08:00:39Z")

</div>

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