gpt4 book ai didi

sorting - Elasticsearch确切结果不会在第一时间返回

转载 作者:行者123 更新时间:2023-12-02 22:53:27 26 4
gpt4 key购买 nike

我添加了一些产品,例如“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索引products_idx1并输入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"
}
使用其他更简单的匹配子句的搜索查询
{
"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"
}
}
]

关于sorting - Elasticsearch确切结果不会在第一时间返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64129903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com