I have added some products like 'lcd apple iphone 11' 'lcd apple iphone x' 'lcd apple iphone xs' 'lcd apple iphone xr' 'lcd samsung s8' 'lcd samsung s8+' 'lcd apple iphone xs max' 'lcd apple iphone xr battery' we had added iphone xr product in last so
I have created elasticsearch index products_idx1
and type product
.
When I search product like apple iphone xr
so it returns iphone xr but not come at top result.
What I want Exact result should be first then partial result should be after exact result .I want a sort the result based on accuracy result.
Here is my code in php elasticsearch
<?php
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';
$client = ClientBuilder::create()->build();
$values =['name','name.prefix','name.suffix','sku'];
$params =
[
'client'=>['verify'=>1,'connect_timeout'=>5],
'from'=> 0,
'size'=>25,
'body' =>[
'query' => [
'bool'=>
[
'should'=> [[
'multi_match'=> ['query'=>'apple iphone xr','type'=>'cross_fields','fields'=>$values,'operator'=>'AND']
],
['match'=>['all'=>['query'=>'apple iphone xr','operator'=>'AND','fuzziness'=>'AUTO'] ]]
]
]
],
'sort'=>['_score'=>['order'=>'desc']],
],
'index'=>'products_idx1'
];
$response = $client->search($params);
echo "<pre>";print_r($response);
while bhavya's answer is working but its more complicated as its using the
match_phrase
query which is more complicated and might be more costly based on the dataset you might have, I have created a simpler version of this query which works.Index sample docs
Search query which uses additional simpler match clause
And search result showing
iphone xr
at top, with more score