fuse.js vs lunr vs flexsearch vs search-index vs elasticlunr
JavaScript 搜尋庫
fuse.jslunrflexsearchsearch-indexelasticlunr類似套件:

JavaScript 搜尋庫

這些搜尋庫提供了不同的搜尋功能和性能,幫助開發者在應用程式中實現高效的搜尋功能。它們各自有不同的設計原則、特性和使用場景,適合不同的需求和應用場景。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
fuse.js20,262,76720,534417 kB264 個月前Apache-2.0
lunr9,480,4009,202-1306 年前MIT
flexsearch1,288,66613,8242.33 MB401 年前Apache-2.0
search-index54,1371,421722 kB113 個月前MIT
elasticlunr26,6162,064-7710 年前MIT

功能比較: fuse.js vs lunr vs flexsearch vs search-index vs elasticlunr

性能

  • fuse.js:

    fuse.js 的性能在處理小型資料集時表現良好,但在大型資料集上可能會因為模糊搜尋而影響性能。

  • lunr:

    lunr 提供了良好的搜尋性能,適合中小型資料集,並且能夠在客戶端快速構建索引。

  • flexsearch:

    flexsearch 以其極高的性能著稱,能夠在毫秒級別內完成搜尋,特別適合需要快速響應的應用。

  • search-index:

    search-index 在伺服器端搜尋時性能優異,適合處理大量資料並支持持久化。

  • elasticlunr:

    elasticlunr 提供了基本的搜尋功能,適合小型資料集,性能良好,但在大型資料集上可能會出現性能瓶頸。

使用場景

  • fuse.js:

    適合需要模糊搜尋的場景,例如用戶輸入不準確的情況,能夠提供更靈活的搜尋結果。

  • lunr:

    適合中小型應用,當需要在客戶端進行搜尋並且資料量適中時,提供簡單易用的解決方案。

  • flexsearch:

    適合需要高性能搜尋的應用,特別是需要多語言支持的情況,適合大型資料集。

  • search-index:

    適合大型應用,尤其是需要持久化資料的情況,能夠在伺服器端進行高效搜尋。

  • elasticlunr:

    適合用於靜態網站或小型應用,當資料量不大且需要快速實現搜尋功能時非常合適。

設計原則

  • fuse.js:

    fuse.js 的設計重點在於模糊搜尋和加權排序,能夠根據用戶需求調整搜尋結果。

  • lunr:

    lunr 提供了一個簡單的 API,易於集成,並且支持客戶端索引構建,適合快速開發。

  • flexsearch:

    flexsearch 設計上注重性能和靈活性,支持多種分詞和自定義配置,適合高需求的應用。

  • search-index:

    search-index 設計上支持持久化和伺服器端搜尋,適合需要長期存儲和查詢的應用。

  • elasticlunr:

    elasticlunr 設計簡單,易於使用,適合快速上手,並且支持基本的搜尋功能。

擴展性

  • fuse.js:

    fuse.js 支持自定義搜尋選項,開發者可以根據具體需求調整搜尋算法和結果排序。

  • lunr:

    lunr 提供了一些擴展功能,但主要集中在基本的搜尋功能上,擴展性相對有限。

  • flexsearch:

    flexsearch 提供了多種擴展選項,開發者可以根據需求自定義分詞和搜尋邏輯,靈活性高。

  • search-index:

    search-index 提供了豐富的擴展功能,支持多種資料來源和持久化選項,適合複雜的應用需求。

  • elasticlunr:

    elasticlunr 支持基本的擴展,但在功能上相對有限,主要適合簡單的搜尋需求。

學習曲線

  • fuse.js:

    fuse.js 的學習曲線相對較低,易於理解和使用,適合快速實現模糊搜尋。

  • lunr:

    lunr 的學習曲線平緩,簡單易用,適合初學者和中小型項目。

  • flexsearch:

    flexsearch 的學習曲線稍陡,因為其功能強大且配置靈活,需要一定的時間來熟悉。

  • search-index:

    search-index 的學習曲線較陡,因為其功能複雜,適合有經驗的開發者使用。

  • elasticlunr:

    elasticlunr 的學習曲線較平緩,適合初學者快速上手,文檔清晰易懂。

如何選擇: fuse.js vs lunr vs flexsearch vs search-index vs elasticlunr

  • fuse.js:

    選擇 fuse.js 如果你需要模糊搜尋功能,並且希望能夠對搜尋結果進行加權排序,這是處理不確定性搜尋的理想選擇。

  • lunr:

    選擇 lunr 如果你需要一個簡單易用的搜尋解決方案,並且希望能夠在客戶端構建索引,適合中小型應用。

  • flexsearch:

    選擇 flexsearch 如果你需要極高的搜尋性能,並且希望支持多語言和自定義分詞,這個庫在速度和靈活性上表現優異。

  • search-index:

    選擇 search-index 如果你需要一個支持持久化的搜尋解決方案,並且希望能夠在伺服器端進行搜尋,適合大型應用和需要持久化資料的場景。

  • elasticlunr:

    選擇 elasticlunr 如果你需要一個輕量級的搜尋庫,並且希望能夠在客戶端進行全文搜尋,特別適合小型應用或靜態網站。

fuse.js 的 README

Fuse.js

Node.js CI Version Downloads code style: prettier Contributors License

Fuse.js is a lightweight, zero-dependency fuzzy-search library written in TypeScript. It works in the browser and on the server, and is designed for searching small-to-medium datasets on the client side where you can't rely on a dedicated search backend.

✨ What's New: Token Search

Multi-word fuzzy search with relevance ranking. Type "javascrpt paterns" and find "JavaScript Patterns" — typo tolerance, multiple words, and smart ranking all at once.

const fuse = new Fuse(docs, {
  useTokenSearch: true,
  keys: ['title', 'author', 'description']
})

fuse.search('javascrpt paterns')
// → [{ item: { title: 'JavaScript Patterns', ... } }]

See Token Search below for details.

Web Workers

Search large datasets without freezing the UI. FuseWorker splits your data across multiple Web Workers and searches in parallel — ~5x faster on 100K documents.

import { FuseWorker } from 'fuse.js/worker'

const fuse = new FuseWorker(docs, {
  keys: ['title', 'author', 'description']
})

const results = await fuse.search('query')
fuse.terminate()

Same options and results as Fuse — just async. Function-valued options (sortFn, getFn, keys[].getFn) aren't supported because functions can't be transferred to a worker; everything else carries over. See the Web Workers docs for the interactive demo and full API.

Installation

npm install fuse.js
yarn add fuse.js

Or include directly via CDN:

<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.mjs"></script>

Quick Start

import Fuse from 'fuse.js'

const books = [
  { title: "Old Man's War", author: 'John Scalzi' },
  { title: 'The Lock Artist', author: 'Steve Hamilton' },
  { title: 'HTML5', author: 'Remy Sharp' },
  { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' }
]

const fuse = new Fuse(books, {
  keys: ['title', 'author']
})

fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', ... }, ... }]

Features

Fuzzy Search

The core of Fuse.js. Uses the Bitap algorithm for approximate string matching — handles typos, misspellings, and partial matches out of the box.

fuse.search('javscript')
// → [{ item: { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' } }]

Weighted Keys

Search across multiple fields with different importance levels. Title matches can rank higher than description matches.

const fuse = new Fuse(docs, {
  keys: [
    { name: 'title', weight: 2 },
    { name: 'description', weight: 1 }
  ]
})

Extended Search

Use operators for precise control: exact match (=), prefix (^), suffix (!), and more. Enable with useExtendedSearch: true.

const fuse = new Fuse(list, {
  useExtendedSearch: true,
  keys: ['title']
})

fuse.search('=exact match')   // exact match
fuse.search('^prefix')        // starts with
fuse.search('!term')          // does not include

Token Search

Splits multi-word queries into individual terms, fuzzy-matches each independently, and ranks results using BM25-style IDF weighting. Enable with useTokenSearch: true.

const fuse = new Fuse(docs, {
  useTokenSearch: true,
  keys: ['title', 'body']
})

fuse.search('express midleware rout')
// Finds "Express Middleware" and "Express Routing Guide" despite typos
  • Typo tolerance per word — each term is fuzzy-matched independently
  • Relevance ranking — rare terms are weighted higher than common ones
  • Word order independent — "patterns javascript" and "javascript patterns" return identical results
  • No query length limit — long multi-word queries work naturally since each term is searched separately
  • AND or OR — tokenMatch: 'all' returns only records matching every word (filtering); the default 'any' matches any word
  • Custom tokenizer — pass a regex or function via tokenize for tokens with internal punctuation (node.js, c++), or use Intl.Segmenter for CJK / Thai word segmentation. Unicode-aware by default

Available in the full build. See the Token Search docs for details and performance benchmarks.

Logical Search

Combine conditions with $and and $or for complex queries. Available in the full build.

fuse.search({
  $and: [
    { title: 'javascript' },
    { author: 'crockford' }
  ]
})

Match Highlighting

Get character-level match indices for highlighting search results in your UI.

const fuse = new Fuse(list, {
  includeMatches: true,
  keys: ['title']
})

const result = fuse.search('javscript')
// result[0].matches[0].indices → [[0, 9]]

Single String Matching

Use Fuse.match() to fuzzy-match a pattern against a single string without creating an index. Useful for one-off comparisons or custom filtering.

const result = Fuse.match('javscript', 'JavaScript: The Good Parts')
// → { isMatch: true, score: 0.04, indices: [[0, 9]] }

Fuse.match() does not support useTokenSearch — token search requires corpus-level statistics (df, fieldCount) that a one-off string comparison can't provide. Passing useTokenSearch: true throws an explicit error. Use new Fuse(docs, { useTokenSearch: true }).search(query) for token-search behavior.

Dynamic Collections

Add and remove documents from a live index without rebuilding.

fuse.add({ title: 'New Book', author: 'New Author' })
fuse.remove((doc) => doc.title === 'Old Book')

Builds

Fuse.js ships in two variants:

BuildIncludesMin + gzip
FullFuzzy + Extended + Logical + Token search~8.6 kB
BasicFuzzy search only~6.8 kB

Use the basic build if you only need fuzzy search and want the smallest bundle size.

Documentation

For the full API reference, configuration options, scoring theory, and interactive demos, visit fusejs.io.

Official ports

  • fuse-swift: Swift port for iOS, macOS, tvOS, watchOS, visionOS, and Linux. Byte-equivalent results, idiomatic Swift API, syncs with each upstream release. Currently in 2.0.0-rc.1.

Supporting Fuse.js

Develop

See DEVELOPERS.md for setup, scripts, and project structure.

Contribute

See CONTRIBUTING.md for guidelines on issues and pull requests.