I cannot figure out the following:
A database with address fields (country, county, city) and an "operate" field, options: locally, nationally internationally
I want to find matches based on location and the "operate" status. So I am basically searching for how to use MySQL OR in ElasticSearch. To my understanding this is "should".
So, any entry that operates internationally should be in the results, any entry that operates nationally in the same country and any entry that operates locally in the same county regardless which city is selected
Just as a test case, I tried several things among which the following:
$arrayinternationally[] = array('match' => array('operate' => 'internationally'));
$arrayNationally[] = array('match' => array('operate' => 'nationally'));
$arrayNationally[] = array('match' => array('country' => '60'));
$arrayLocally[] = array('match' => array('operate' => 'locally'));
$arrayLocally[] = array('match' => array('country' => 'UK'));
$arrayLocally[] = array('match' => array('county' => '60'));
$params = [
'index' => 'bzlistings',
'body' => [
'from' => 0,
'size' => 80,
'query' => [
'bool' => [
'should' => [
'bool' => [
'should' => $arrayinternationally
]
],
'should' => [
'bool' => [
'should' => $arrayNationally
]
],
'should' => [
'bool' => [
'should' => $arrayLocally
]
],
],
],
],
];
Those entries in another country with "operate" set to "internationally", are not included, which is wrong.
How can this be done in ElasticSearch?
Thanks, Peter
CodePudding user response:
I think there is something wrong with the bool queries. Could you test with following query :
$params = [
'index' => 'bzlistings',
'body' => [
'from' => 0,
'size' => 80,
'query' => [
'bool' => [
'should' => [
[
'bool' => [
'must' => $arrayinternationally
]
],
[
'bool' => [
'must' => $arrayNationally
]
],
[
'bool' => [
'must' => $arrayLocally
]
],
],
],
],
],
];
But I think, in your case, inner bools need to be must
instead of should
. Also, I recommend that if you are looking for exact matching, you can use term
query instead of match
.
Typically, the bool query structure is something like the below :
GET sample-index/_search
{
"query": {
"bool": {
"should": [
{},
{}
]
}
}
}