# AND between must and should

**URL:** <https://discuss.elastic.co/t/and-between-must-and-should/169471>\
**Category:** Elasticsearch\
**Created:** [February 21, 2019, 5:31pm UTC](https://discuss.elastic.co/t/and-between-must-and-should/169471 "2019-02-21T17:31:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio\_Batalha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabio_batalha/32/4229_2.png) [@Fabio\_Batalha](https://discuss.elastic.co/u/Fabio_Batalha)\
**Post date:** [February 21, 2019, 5:31pm UTC](https://discuss.elastic.co/t/and-between-must-and-should/169471/1 "2019-02-21T17:31:02Z")

</div>

Hello,

I would like to know how to put an AND operator between a MUST and SHOULD.

As for example. In the query bellow I want to have all the documents with the given IP addresses and at least one of the timestamp ranges in the SHOULD.

At this moment, if none of the ranges in SHOULD are valid, the records of the MUST queries are being still retrieved.

GET /site\_usage/usage\_events/\_search  
{  
"query": {  
"bool": {  
"must": [  
{"terms": {"geoip.ip": ["136.187.96.170", "136.187.96.171"]}}  
],  
"should": [  
{"range": {"timestamp": {"gte": "2019-01-15", "lte": "2019-01-17", "format": "yyyy-MM-dd"}}},  
{"range": {"timestamp": {"gte": "2019-01-22", "lte": "2019-01-23", "format": "yyyy-MM-dd"}}}  
]  
}  
},  
"size": 0  
}

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)\
**Post date:** [February 21, 2019, 7:33pm UTC](https://discuss.elastic.co/t/and-between-must-and-should/169471/2 "2019-02-21T19:33:29Z")

</div>

you can do something like:

```
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                #query from original must block
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                #query from original should block
              }
            ]
          }
        }
      ]
    }
  }
}
```

---

<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:** [March 21, 2019, 7:33pm UTC](https://discuss.elastic.co/t/and-between-must-and-should/169471/3 "2019-03-21T19:33:33Z")

</div>

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