首页 > 解决方案 > 通过整数字段提升结果

问题描述

我正在尝试创建和自动完成目的地,并且我想通过流行整数字段来提高结果。

我正在尝试使用此 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 的第一个位置。)

我想用整数值添加提升热门城市的容量。

有什么建议吗?

谢谢!

标签: elasticsearchboostautocomplete

解决方案


默认情况下,您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
                ]

推荐阅读