I have to create the following elastic search query in php:
$parameters = [
'index' => 'myindex',
'from' => 0,
'size' => 60,
'body' => [
'query' => [
'bool' => [
'filter' => [[
'bool' => [
'must' => [
$myvariable,
['nested' => [
'path' => 'sh',
'query' => [
'bool' => [
'filter' => [
['term' => ['sh.keyword' => 'PK']]
]
]
]
]
]
]
]
]],
]
],
"sort" => [
["score" => ["mode" => "avg"]]
]
]
];
Where $myvariable should be:
['bool'=>
['should'=>[
['term'=> ['id'=> {$id1}]],
['term'=> ['id'=> {$id2}]],
],
'minimum_should_match'=> 1
]
]
I do not want to hardcode the values as it completely depends on the user whether she/he will choose the mentioned values in drop down or not.
What I tried?
I tried to create the inner part, that is the term part:
$countofids = count($combined_array['ids']);
if ($countofids != 0) {
for ($i = 0; $i < $countofids ; $i++) {
if ($i == 0) {
$myvariable= array('term' => array('id' => $combined_array['ids'][$i]));
} else {
array_push($myvariable, $combined_array['ids'][$i]);
}
}
}
But, it does not work as expected. I get:
['term'=> ['id'=> {$id1}]]
But, further, I do not get the required result. Moreover, I don't know how to separate them by comma. Can anyone provide a better solution?