【问题标题】:How to weight items in a fuzzy search如何在模糊搜索中对项目进行加权
【发布时间】:2018-03-13 20:52:39
【问题描述】:

使用Fuse.js,我需要对单个项目进行加权,以便在搜索结果中获得更好的排名。例如,如何确保“Paris France”在使用以下数据的“Paris”查询中得分最高?

places = [{
  name: 'Paris, France'
  weigth: 10
},{
  name: 'Paris, Ontario'
  weigth: 2
},
{
  name: 'Paris, Texas'
  weigth: 1
}]

【问题讨论】:

    标签: javascript ranking fuzzy-search fuse.js


    【解决方案1】:

    据我所知,Fuse.js 中没有内置方法来执行此操作。 weight 属性旨在应用于正在搜索的属性(在 options 对象中),而不是应用于正在搜索的对象(如示例 here 中所示。

    我可能建议自己编写一个函数来对其进行排序。所以一旦你得到你的结果数组,在搜索之后,自己执行一个Array.sort()(文档here)。

    例如...

    //Your places object
    var places = [
      {
        name: 'Paris, Texas',
        weight: 2
      },
      {
        name: 'Paris, France',
        weight: 10
      },
      {
        name: 'Paris, Texas',
        weight: 1
      }
    ];
    
    //Your search options
    var options = {
      keys: [
        "name"
      ]
    };
    var fuse = new Fuse(places, options); // "list" is the item array
    var result = fuse.search("Paris");
    
    //Once you have got this result, perform your own sort on it:
    
    result.sort(function(a, b) {
        return b.weight - a.weight;
    });
    
    console.log('Your sorted results:');
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.1.0/fuse.min.js"></script>

    【讨论】:

    • 进行第二次排序是一个很好的补丁。我还试验了 Fuse 返回的质量分数,并使用权重作为乘数。否则,第二类可能最终会出现其他糟糕的匹配项。
    猜你喜欢
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2012-11-10
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    相关资源
    最近更新 更多