模糊搜索

128 阅读1分钟

安装 fuse

npm install --save fuse.js@6.4.6


/**
 * 搜索库相关
 */
 // list 为 数据源
const fuse = new Fuse(list, {
    // 是否按优先级进行排序
    shouldSort: true,
    // 匹配长度超过这个值的才会被认为是匹配的
    minMatchCharLength: 1,
    // 将被搜索的键列表。 这支持嵌套路径、加权搜索、在字符串和对象数组中搜索。
    // name:搜索的键
    // weight:对应的权重
    keys: [
      {
        name: 'title',
        weight: 0.7
      },
      {
        name: 'path',
        weight: 0.3
      }
    ]
  })


数据源格式

[  {     path:xxx,     title:[]
  }
]