# Bool Query Not Working as Expected

**URL:** <https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228>\
**Category:** Elasticsearch\
**Created:** [November 18, 2017, 8:03am UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228 "2017-11-18T08:03:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbushid0](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@mrbushid0](https://discuss.elastic.co/u/mrbushid0)\
**Post date:** [November 18, 2017, 8:03am UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/1 "2017-11-18T08:03:35Z")

</div>

I'm trying to write a simple query in ES 5.6.4 to be the equivalent of "SELECT \* FROM index WHERE Location LIKE '%Michigan%' OR Location LIKE '%Main%', and it's working but the second term in the "or" does not match as many results as when I use it in an "or" by itself ? Which doesn't make any sense to me, for Example:

{  
"query":{  
"bool":{  
"must":{  
"bool":{  
"should":[  
{  
"match\_phrase":{  
"Location":"Michigan"  
}  
},  
{  
"match\_phrase":{  
"Location":"Main"  
}  
}  
]  
}  
}  
}  
}  
}

Will return :  
{"Location":"58391 Main","City":"New Haven"}

BUT:

{  
"query":{  
"bool":{  
"must":{  
"bool":{  
"should":[  
{  
"match\_phrase":{  
"Location":"Main"  
}  
}  
]  
}  
}  
}  
}  
}

returns:

{"Location":"419 S. Main Street","City":"Royal Oak"}  
{"Location":"511 S. Main St.","City":"Royal Oak"}  
{"Location":"58391 Main","City":"New Haven"}

etc...

Any assistance is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 18, 2017, 11:29am UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/2 "2017-11-18T11:29:18Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

Why don't you run a simple

```auto
  "match": {
    "Location": "Michigan Main"
  }

```

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [November 18, 2017, 6:36pm UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/3 "2017-11-18T18:36:03Z")

</div>

Hi,

Note that you can have only 1 bool query with 2 should and a minimum\_should\_match = 1, instead of a must with 2 bool. With you first query you have only 1 result ?

Xavier

---

<div class="post-metadata">

**Author:** ![mrbushid0](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@mrbushid0](https://discuss.elastic.co/u/mrbushid0)\
**Post date:** [November 18, 2017, 7:01pm UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/4 "2017-11-18T19:01:24Z")

</div>

Thanks for replying,

I'm using curl, I'm just trying to run a query to match "SELECT \* FROM index WHERE Location LIKE '%Michigan%' OR Location LIKE '%Main%'". My goal is to retrieve every building containing Michigan OR Main in the "Location" term.

I tried running the script you suggested and got the same result unfortunately. It returned many results for "Michigan" but only one result for "Main":

"Michigan":  
{Location":"13530 Michigan Avenue","City":"Dearborn"}  
{"Location":"118 E. Michigan Avenue","City":"Saline"}  
{"Location":"Lot 1 Michigan Avenue","City":"Saline"}

"Main":  
{"Location":"58391 Main","City":"New Haven"}

I know there are other locations with Main in them because if I run :

```
curl -XGET localhost:9200/test/_search -d '
{ 
"query": { 
"match": { 
"Location": "Main" 
} 
 
} 
}'

```

I get many results for "Main", I'm wondering if this having to do with the order at which Main is in the "Location" term ?

here is a simplified version of my full script:

```
curl -XGET localhost:9200/test/_search -d '
{
"query":{
"bool":{
"should":[
{
"match_phrase":{
"Location":"Michigan"
}
},
{
"match_phrase":{
"Location":"Main"
}
}
]
}
}
}'
```

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [November 19, 2017, 12:01pm UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/5 "2017-11-19T12:01:19Z")

</div>

Hi,

When you run your query with the "OR", how many documents do you receive ? The default size of a query is 10. It's possible that ES return 9 Michigan et 1 Main ?

@see [https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-from-size.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-from-size.html)

---

<div class="post-metadata">

**Author:** ![mrbushid0](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@mrbushid0](https://discuss.elastic.co/u/mrbushid0)\
**Post date:** [November 19, 2017, 4:54pm UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/6 "2017-11-19T16:54:03Z")

</div>

Thanks Xavier,

That solved my issue!

---

<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:** [December 17, 2017, 4:54pm UTC](https://discuss.elastic.co/t/bool-query-not-working-as-expected/108228/7 "2017-12-17T16:54:22Z")

</div>

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