elasticsearch - 通过整数字段提升结果
问题描述
我正在尝试创建和自动完成目的地,并且我想通过流行整数字段来提高结果。
我正在尝试使用此 function_score 查询
'query' => [
'function_score' => [
'query' => [
"bool" => [
"should" => [
[
"multi_match"=>[
"query"=>$text,
"fields"=>[
"destination_name_*"
],
"type"=>"most_fields",
"boost" => 2
]
],
[
"multi_match"=>[
"query"=>$text,
"fields"=>[
"destination_name_*"
],
"fuzziness" => "1",
"prefix_length"=> 2
]
],
[
"multi_match"=>[
"query"=>$text,
"fields"=>[
"destination_name_*.exact"
],
"boost" => 2
]
]
]
]
],
'field_value_factor' => [
'field'=>'popularity'
]
],
],
映射和设置:
'settings' => [
'analysis' => [
'filter' => [
'ngram_filter' => [
'type' => 'edge_ngram',
'min_gram' => 2,
'max_gram' => 20,
]
],
'analyzer' => [
'ngram_analyzer' => [
'type' => 'custom',
"tokenizer" => "standard",
'filter' => ['lowercase', 'ngram_filter'],
]
]
],
],
'mappings' =>[
'doc' => [
"properties"=> [
"destination_name_en"=> [
"type"=> "text",
"term_vector"=> "yes",
"analyzer"=> "ngram_analyzer",
"search_analyzer"=> "standard",
"fields" => [
"exact" => [
"type" => "text",
"analyzer" => "standard"
]
]
],
"destination_name_es"=> [
"type"=> "text",
"term_vector"=> "yes",
"analyzer"=> "ngram_analyzer",
"search_analyzer"=> "standard",
"fields" => [
"exact" => [
"type" => "text",
"analyzer" => "standard"
]
]
],
"destination_name_pt"=> [
"type"=> "text",
"term_vector"=> "yes",
"analyzer"=> "ngram_analyzer",
"search_analyzer"=> "standard",
"fields" => [
"exact" => [
"type" => "text",
"analyzer" => "standard"
]
]
],
"popularity"=> [
"type"=> "integer",
]
]
]
]
我将cancún 的流行度值设置为10,当我开始写“ca”时,第一个选项是cancún。这项工作符合预期...
但是当我试图找到其他流行值为 0 的城市(如巴亚尔塔港)时,问题就来了。当我写“Puerto Va”时,我得到以下结果:
1.-Val d´Aosta 2.-Puerto Lopez 3.-布里斯托尔 - 弗吉尼亚州和许多其他......(但不是巴亚尔塔港)
需要强调的是,在没有函数分数和 field_value_factor 的情况下,此查询按预期工作(返回 puerto vallarta 的第一个位置。)
我想用整数值添加提升热门城市的容量。
有什么建议吗?
谢谢!
解决方案
默认情况下,您field_value_factor
会将自然分数乘以 field 的值popularity
。因此,如果值为 0,Puerto Vallarta
则其分数将始终为 0。它将匹配但永远不会出现在第一个结果中。
另外,您的提升将是线性的,这肯定不是您想要的,因为热门城市将完全压倒结果列表。
然后,您应该在此处使用modifier
字段值因子doc的属性。
如果您将其设置为log2p
它应该按预期工作。在应用对数函数之前,修饰符log2p
会将字段值加 2 。popularity
那么2人气城市和4人气城市之间的提升差异将是明智的。但是当人气分数上升时差异会减小
前任 :
popularity 2 => log(4) => 0.6
popularity 4 => log(6) => 0.77
popularity 20 => log(22) => 1.34
popularity 22 => log(24) => 1.38
将此添加到您的查询中:
'field_value_factor' => [
'field'=>'popularity',
'modifier' => 'log2p' <== add this
]
推荐阅读
- angular - AWS Amplify 和 Angular 7 的 AOT 运行时错误 - 未定义 API
- eclipse-plugin - 使用我们的编辑器插件时,删除按钮偶尔会停止工作,仅在 Windows 上
- c++ - 客户端未检测到服务器断开连接
- mariadb - A Database Error Occurred 错误编号:1064 第 34 行
- momentjs - 角力矩持续时间 - 类型“持续时间”上不存在属性“_data”
- java - 使用 Javacv 提高人脸识别的准确性
- python - 尝试将 CI 添加到 python 项目时出错
- javascript - 使用 Electron 将 Angular 框架应用程序转换为桌面应用程序
- android-sqlite - 在 SQLite Android Studio 中更新单行
- jenkins - 通过 SVN 从 Jenkins 更新工作区时出现间歇性“SVNException: svn: E175002: Connection reset”