Bool Query Not Working as Expected


(Andrew) #1

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.


(David Pilato) #2

Please format your code using </> icon as explained in this guide. It will make your post more readable.

Or use markdown style like:

```
CODE
```

Why don't you run a simple

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

Could you provide a full recreation script as described in

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


(Xavier Facq) #3

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


(Andrew) #4

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"
}
}
]
}
}
}'

(Xavier Facq) #5

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


(Andrew) #6

Thanks Xavier,

That solved my issue!


(system) closed #7

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