# Removing redundancy in query

**URL:** <https://discuss.elastic.co/t/removing-redundancy-in-query/335778>\
**Category:** Elasticsearch\
**Created:** [June 12, 2023, 2:10pm UTC](https://discuss.elastic.co/t/removing-redundancy-in-query/335778 "2023-06-12T14:10:24Z")\
**Posts on this page:** 4\
**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 12, 2023, 2:10pm UTC](https://discuss.elastic.co/t/removing-redundancy-in-query/335778/1 "2023-06-12T14:10:24Z")

</div>

Can this query:

{  
"query": {  
"bool": {  
"must": [  
{ "match": { "title": "elasticsearch" } },  
{ "match": { "author": "jondoe" } },  
{ "range": { "price": { "gte": 10, "lte": 100 } } }  
],  
"must\_not": [  
{ "match": { "author": "jondoe" } },  
{ "match": { "cover": "black" } }  
]  
}  
}  
}

be optimized (removing redundancy) by rewriting it like this:

{  
"query": {  
"bool": {  
"must": [  
{ "match": { "title": "elasticsearch" } },  
{ "range": { "price": { "gte": 10, "lte": 100 } } },  
{ "match": { "author": "jondoe" } }  
],  
"must\_not": [  
{ "match": { "cover": "black" } }  
]  
}  
}  
}

1/ I want to understand if this is possible  
2/ would it help with reducing processing time

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [June 12, 2023, 3:39pm UTC](https://discuss.elastic.co/t/removing-redundancy-in-query/335778/2 "2023-06-12T15:39:45Z")

</div>

Hi @searchwithme,

The key difference between your 2 queries looks to be the inclusion of

> [@searchwithme](#):
>
> { "match": { "author": "jondoe" } },

in the `must` and `must_not` clauses.

Have you compared the queries using the [Search Profiler](https://www.elastic.co/guide/en/kibana/current/xpack-profiler.html) and checked the results returned by each query? Can you explain more what documents you expect to return?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [June 13, 2023, 7:38am UTC](https://discuss.elastic.co/t/removing-redundancy-in-query/335778/3 "2023-06-13T07:38:56Z")

</div>

In this particular case with the conflicting clauses (jondoe must and must not exist) it would be impossible for an optimisation algorithm to automatically know which of the 2 clauses you would want to keep.  
Assisting with change rather than automating would be of use here and one area I have experimented with is using custom aggregations and [Sankey diagrams](https://youtu.be/kvdI_H3nPCs?t=2320) to show how individual clauses in a given query match (or don’t) and this would have red-flagged your must\_not clause as rejecting 100% of the documents fed to it by all the other query clauses.

---

<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 11, 2023, 7:39am UTC](https://discuss.elastic.co/t/removing-redundancy-in-query/335778/4 "2023-07-11T07:39:49Z")

</div>

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