我添加了一些产品,例如“ lcd苹果iphone 11”,“ lcd苹果iphone x”,“ lcd苹果iphone xs”,“ lcd苹果iphone xr”,“ lcd三星s8”,“ lcd三星s8 +”,“ lcd苹果iphone xs max”,“ lcd苹果” “ iPhone XR电池”,我们最近添加了iPhone XR产品,因此
我已经创建了elasticsearch indexproducts_idx1
和type product
。
当我搜索这样的产品时apple iphone xr
,它会返回iphone xr,但不会获得最佳结果。
我想要的是确切结果,然后是部分结果应在精确结果之后。我要根据准确性结果对结果进行排序。
这是我在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);
虽然bhavya的答案有效,但由于使用match_phrase
查询更为复杂(根据您可能拥有的数据集,查询可能更复杂且成本更高),我为此查询创建了一个更简单的版本。
索引样本文档
{
"title" : "lcd apple iphone xr"
}
{
"title" : "lcd apple iphone 11"
}
{
"title" : "lcd apple iphone x"
}
{
"title" : "lcd apple iphone xs"
}
{
"title" : "iphone xr"
}
使用其他更简单的match子句的搜索查询
{
"query": {
"bool": {
"should": [
{
"match": {
"title" : "apple iphone xr"
}
},
{
"match": { --> simple additional match clause solves issue.
"title": "iphone xr"
}
}
]
}
}
}
搜索结果显示iphone xr
在顶部,得分更高
"hits": [
{
"_index": "64129903",
"_type": "_doc",
"_id": "6",
"_score": 1.8347404, // note score is higher than other results.
"_source": {
"title": "iphone xr"
}
},
{
"_index": "64129903",
"_type": "_doc",
"_id": "2",
"_score": 1.8268716,
"_source": {
"title": "lcd apple iphone xr"
}
},
{
"_index": "64129903",
"_type": "_doc",
"_id": "3",
"_score": 0.54542315,
"_source": {
"title": "lcd apple iphone 11"
}
},
{
"_index": "64129903",
"_type": "_doc",
"_id": "4",
"_score": 0.54542315,
"_source": {
"title": "lcd apple iphone x"
}
},
{
"_index": "64129903",
"_type": "_doc",
"_id": "5",
"_score": 0.54542315,
"_source": {
"title": "lcd apple iphone xs"
}
}
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句