# Help optimize my query

**URL:** <https://discuss.elastic.co/t/help-optimize-my-query/335250>\
**Category:** Elasticsearch\
**Created:** [June 5, 2023, 5:31pm UTC](https://discuss.elastic.co/t/help-optimize-my-query/335250 "2023-06-05T17:31:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![searchwithme](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/searchwithme/32/137075_2.png) [@searchwithme](https://discuss.elastic.co/u/searchwithme)\
**Post date:** [June 5, 2023, 5:31pm UTC](https://discuss.elastic.co/t/help-optimize-my-query/335250/1 "2023-06-05T17:31:24Z")

</div>

I have this query:

"query": {  
"bool": {  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"movies-date": {  
"gt": "2018",  
"lt": "2022"  
}  
}  
},

given that this is a must query, does it make sense to move the range outside of the bool (as must is like an AND condition). However, I am not sure how to reorganize the query.

Can someone please help?

---

<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:** [June 5, 2023, 5:44pm UTC](https://discuss.elastic.co/t/help-optimize-my-query/335250/2 "2023-06-05T17:44:52Z")

</div>

Hi @searchwithme

You didn't give much detail, but you could rearrange it like this:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "movies-date": {
              "gt": "2018",
              "lt": "2022"
            }
          }
        }
      ],
      "must": [
        {
          .....
        }
      ]
    }
  }
}

```

---

<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:** [July 3, 2023, 5:45pm UTC](https://discuss.elastic.co/t/help-optimize-my-query/335250/3 "2023-07-03T17:45:46Z")

</div>

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