【问题标题】:Elastic Search 6.4.2 combine fuzziness search with exact searchElastic Search 6.4.2 结合了模糊搜索和精确搜索
【发布时间】:2018-10-23 12:54:54
【问题描述】:

Laravel 的一个项目,更详细:用于Laravel Nova 搜索。 我在ES 中有一个模型规则,用于查找特定的Model 记录。它有以下规则

namespace App\Elastic\Rules;

use ScoutElastic\SearchRule;

class BusinessSearch extends SearchRule

/**
 * @inheritdoc
 */
public function buildHighlightPayload()
{
    return [
        'fields' => [
            'name' => [
                'type' => 'plain'
            ]
        ]
    ];
}

/**
 * @inheritdoc
 */
public function buildQueryPayload()
{
    $query = $this->builder->query;

    return [
        'should' => [
            [
                'multi_match' => [
                    'query'     => $query,
                    'fuzziness' => 5
                ]
            ],
            [
                'nested' => [
                    'path' => 'categories',
                    'query' => [
                        'bool' => [
                            'must' => [
                                'match' => [
                                    'categories.name' => $query
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];
}

我需要以某种方式向此功能添加以下内容: 当用户在引号中键入一个值时,它必须执行exact search,否则,fuzziness 就像现在一样。关于实施这些东西的任何想法?谢谢。

【问题讨论】:

    标签: laravel elasticsearch search


    【解决方案1】:

    我创建了以下通用查询,而不是根据用户为此场景提供的输入来执行什么查询,我想这足以满足您的要求。

    POST testindex/_search
    {  
       "query":{  
          "bool":{  
             "should":[  
                {  
                   "bool":{  
                      "must":{  
                         "multi_match":{  
                            "query":"something",
                            "fields":[ "field_1", "field_2" ]
                         }
                      }
                   }
                },
                {  
                   "bool":{  
                      "must":{  
                         "multi_match":{  
                            "query":"something",
                            "fields":[  "field_1", "field_2" ],
                            "fuzziness": 5
                         }
                      }
                   }
                }
             ]
          }
       }
    }
    

    为简单起见,上面的查询如下所示

    bool:
      should:
        bool:
        - must [ exact query ]
        bool:
        - must [ fuzzy query ]
    

    这两个查询都将针对所有输入执行,但是如果 exact query 没有给你结果,fuzzy query 会返回给你结果。

    现在,如果exact query 确实返回结果,fuzzy query 也可能会为您提供结果,除非exact query 命中的结果最终会具有更高的relevancy_score,因此会出现在搜索结果的顶部。

    如果有帮助,请告诉我!

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多