# Bool query VS filtered query with query filter

**URL:** <https://discuss.elastic.co/t/bool-query-vs-filtered-query-with-query-filter/11360>\
**Category:** Elasticsearch\
**Created:** [March 28, 2013, 2:54pm UTC](https://discuss.elastic.co/t/bool-query-vs-filtered-query-with-query-filter/11360 "2013-03-28T14:54:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [March 28, 2013, 2:54pm UTC](https://discuss.elastic.co/t/bool-query-vs-filtered-query-with-query-filter/11360/1 "2013-03-28T14:54:44Z")

</div>

Hi list,

In a previous post, I got the following answer to my question:

{  
"query" : {  
"bool" : {  
"should" : [  
{  
"bool" : {  
"must" : [  
{ "field" : { "name" : "do\*" }},  
{ "match" : { "type" : "mammal"}}  
]  
}  
},  
{  
"bool" : {  
"must" : [  
{ "field" : { "name" : "le\*" }},  
{ "match" : { "type" : "reptile"}}  
]  
}  
}  
]  
}  
}  
}

Taking a look at the docs, I see this query could also be expressed as  
(using query filter):

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"or": [  
{  
"and": [  
{  
"query": {  
"field": {  
"name": "do\*"  
}  
}  
},  
{  
"term": {  
"type": "mammal"  
}  
}  
]  
},  
{  
"and": [  
{  
"query": {  
"match": {  
"name": "\*le"  
}  
}  
},  
{  
"term": {  
"type": "reptile"  
}  
}  
]  
}  
]  
}  
}  
}  
}

So which would be the optimal one?

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [March 28, 2013, 8:46pm UTC](https://discuss.elastic.co/t/bool-query-vs-filtered-query-with-query-filter/11360/2 "2013-03-28T20:46:03Z")

</div>

The filtered approach should be faster, especially if the filters are  
cached. However, filters do not contribute to the scoring, so if ranking is  
important, then queries are the way to go.

--  
Ivan

On Thu, Mar 28, 2013 at 7:54 AM, Enrique Medina Montenegro \<  
[e.medina.m@gmail.com](mailto:e.medina.m@gmail.com)\> wrote:

> Hi list,
> 
> In a previous post, I got the following answer to my question:
> 
> {  
> "query" : {  
> "bool" : {  
> "should" : [  
> {  
> "bool" : {  
> "must" : [  
> { "field" : { "name" : "do\*" }},  
> { "match" : { "type" : "mammal"}}  
> ]  
> }  
> },  
> {  
> "bool" : {  
> "must" : [  
> { "field" : { "name" : "le\*" }},  
> { "match" : { "type" : "reptile"}}  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> Taking a look at the docs, I see this query could also be expressed as  
> (using query filter):
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "or": [  
> {  
> "and": [  
> {  
> "query": {  
> "field": {  
> "name": "do\*"  
> }  
> }  
> },  
> {  
> "term": {  
> "type": "mammal"  
> }  
> }  
> ]  
> },  
> {  
> "and": [  
> {  
> "query": {  
> "match": {  
> "name": "\*le"  
> }  
> }  
> },  
> {  
> "term": {  
> "type": "reptile"  
> }  
> }  
> ]  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> So which would be the optimal one?
> 
> Thanks.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:43am UTC](https://discuss.elastic.co/t/bool-query-vs-filtered-query-with-query-filter/11360/3 "2017-07-06T02:43:47Z")

</div>


