← 返回
未分类 中文

Fuse.js Fuzzy Search

Implement fuzzy search in JavaScript/TypeScript projects using Fuse.js. Use when users need client-side search, fuzzy matching, search highlighting, multi-fi...
使用 Fuse.js 在 JavaScript/TypeScript 项目中实现模糊搜索。适用于需要客户端搜索、模糊匹配、搜索高亮、多字段搜索等场景。
openlark openlark 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 202
下载
💾 0
安装
1
版本
#latest

概述

Fuse.js Fuzzy Search

A zero-dependency TypeScript library based on the Bitap algorithm. Works in browsers, Node.js, and Deno.

Installation

npm install fuse.js           # Full version
npm install fuse.js@beta      # Includes FuseWorker
import Fuse from 'fuse.js'          // ESM full version
import Fuse from 'fuse.js/basic'    // Basic version (fuzzy search only)

Minimal Example

const fuse = new Fuse(list, {
  keys: ['title', 'author'],
  includeScore: true,         // Returns score 0-1
  threshold: 0.4,             // 0=exact match, 1=any match (0.3-0.4 recommended for search-as-you-type)
})
fuse.search('jon')            // → [{ item, score, refIndex }]

Usage by Scenario

Fuzzy Search (Basic)

new Fuse(list, {
  keys: ['name'],
  threshold: 0.4,
  ignoreLocation: true,       // MUST enable for long text, otherwise only searches ~first 60 chars
  includeMatches: true,       // For highlighting
  includeScore: true,
  minMatchCharLength: 2,      // Ignores single-character matches
})

Multi-field Weighted Search

keys: [
  { name: 'title', weight: 2 },
  { name: 'description', weight: 1 },
]

Dot notation: 'author.firstName'. Set ignoreFieldNorm to disable the short-field bonus.

Extended Search (Operators)

{ useExtendedSearch: true }

fuse.search('^java =exact')    // space=AND, |=OR
fuse.search("!deprecated | 'active")

Operators: no prefix=fuzzy, ==exact, '=contains, !=exclude, ^=prefix, .$=suffix. Requires Fuse.use(ExtendedSearch) for the basic version.

Tokenized Search

{
  useTokenSearch: true,
  tokenMatch: 'any'           // 'any'(OR, default) | 'all'(AND filtering)
}

Multi-word queries are split into independent tokens for fuzzy matching + IDF weighting. Word order is irrelevant, and there is no 32-character length limit. Use the tokenize function + Intl.Segmenter for CJK tokenization. Full version only.

Logical Queries

fuse.search({
  $and: [{ title: 'js' }, { $or: [{ author: 'a' }, { author: 'b' }] }]
})

Highlighting

includeMatches: trueresult.matches[].indices[[start, end], ...]. Note: end is inclusive.

Dynamic Add/Remove & Single Matching

fuse.add(doc)
fuse.remove(doc => doc.id === 'x')
Fuse.match('patern', 'pattern matching')  // → { isMatch, score, indices }

Web Worker (Large Datasets)

import { FuseWorker } from 'fuse.js/worker'
const fuse = new FuseWorker(docs, { keys: ['title'] })
const r = await fuse.search('q')
fuse.terminate()    // Must clean up

With 4 workers + 100K documents: approximately 3.3x speedup. Browser only. Does not support sortFn/getFn/useTokenSearch.

Reference Documentation

Load as needed:

  • options — Complete options API
  • examples — Practical patterns and scenario quick reference
  • react — React components: debouncing, highlighting, virtualization
  • performance — Benchmark data and optimization guide
  • vs-semantic — Comparison with semantic search and selection guide

Important Notes

  • Default search only looks at the first ~60 characters of a field → Use ignoreLocation: true for long text.
  • Bitap has a single-pattern limit of 32 characters → Use useTokenSearch for long queries.
  • FuseWorker is browser only. The search() method returns a Promise. Does not support function options.
  • Score: 0 = perfect match, 1 = no match.
  • The basic version does not include extended search or tokenized search.

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-26 23:46

安全检测

腾讯云安全 (Keen)

队列中

腾讯云安全 (Sanbu)

队列中

🔗 相关推荐

dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 198 📥 68,309
content-creation

Toutiao Graphic Publisher

openlark
通过浏览器自动化在头条发布图文内容,支持智能排版、自动生成热门标签等功能。
★ 2 📥 1,062
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 687 📥 331,444