Reputation: 3579
I tried to show all the document which matches either title or body section of article in elastic search and i wrote following code in php
if(!empty($_GET))
{
$bodysearch=$_GET['bodysearch'];
$titlesearch=$_GET['titlesearch'];
$params = [
'index' => 'pages',
'type' => 'page',
'body' => [
'query' => [
'bool'=>[
'should'=>[
'match'=>['title'=>$titlesearch],
'match'=>['body'=>$bodysearch]
]
]
]
]
];
$result = $client->search($params)
}
and wrote following to display result
foreach($result['hits']['hits'] as $hit)
{
$sources=$hit['_source'];
echo '<a href="javascript:;" style="display:block;">'.$sources['title'].'</a>';
echo substr($sources['body'], 0,100).'....';
}
It displays only the list document whose content matching body section provided in $bodysearch
, but i want to get the list of document matching $titlesearch
to title or matching $bodysearch
to body content or matching both, what should i do for such.
Upvotes: 0
Views: 24
Reputation: 217424
Your bool/should
is currently an associative array and the second match is overwriting the first one. Use a plain array instead by surrouding your match
queries with [...]
:
$params = [
'index' => 'pages',
'type' => 'page',
'body' => [
'query' => [
'bool'=>[
'should'=>[
[ 'match'=>['title'=>$titlesearch] ],
[ 'match'=>['body'=>$bodysearch] ]
]
]
]
]
];
Upvotes: 1