|
7 | 7 | [](https://github.com/krisk/Fuse/graphs/contributors) |
8 | 8 |  |
9 | 9 |
|
10 | | -## Supporting Fuse.js |
| 10 | +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. |
| 11 | + |
| 12 | +## ✨ What's New: Token Search |
| 13 | + |
| 14 | +Multi-word fuzzy search with relevance ranking. Type `"javascrpt paterns"` and find `"JavaScript Patterns"` — typo tolerance, multiple words, and smart ranking all at once. |
| 15 | + |
| 16 | +```js |
| 17 | +const fuse = new Fuse(docs, { |
| 18 | + useTokenSearch: true, |
| 19 | + keys: ['title', 'author', 'description'] |
| 20 | +}) |
| 21 | + |
| 22 | +fuse.search('javascrpt paterns') |
| 23 | +// → [{ item: { title: 'JavaScript Patterns', ... } }] |
| 24 | +``` |
| 25 | + |
| 26 | +See [Token Search](#token-search) below for details. |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install fuse.js |
| 32 | +``` |
| 33 | + |
| 34 | +```bash |
| 35 | +yarn add fuse.js |
| 36 | +``` |
| 37 | + |
| 38 | +Or include directly via CDN: |
| 39 | + |
| 40 | +```html |
| 41 | +<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.mjs"></script> |
| 42 | +``` |
| 43 | + |
| 44 | +## Quick Start |
| 45 | + |
| 46 | +```js |
| 47 | +import Fuse from 'fuse.js' |
| 48 | + |
| 49 | +const books = [ |
| 50 | + { title: "Old Man's War", author: 'John Scalzi' }, |
| 51 | + { title: 'The Lock Artist', author: 'Steve Hamilton' }, |
| 52 | + { title: 'HTML5', author: 'Remy Sharp' }, |
| 53 | + { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' } |
| 54 | +] |
| 55 | + |
| 56 | +const fuse = new Fuse(books, { |
| 57 | + keys: ['title', 'author'] |
| 58 | +}) |
| 59 | + |
| 60 | +fuse.search('javscript') |
| 61 | +// → [{ item: { title: 'JavaScript: The Good Parts', ... }, ... }] |
| 62 | +``` |
| 63 | + |
| 64 | +## Features |
| 65 | + |
| 66 | +### Fuzzy Search |
| 67 | + |
| 68 | +The core of Fuse.js. Uses the [Bitap algorithm](https://en.wikipedia.org/wiki/Bitap_algorithm) for approximate string matching — handles typos, misspellings, and partial matches out of the box. |
| 69 | + |
| 70 | +```js |
| 71 | +fuse.search('javscript') |
| 72 | +// → [{ item: { title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' } }] |
| 73 | +``` |
| 74 | + |
| 75 | +### Weighted Keys |
| 76 | + |
| 77 | +Search across multiple fields with different importance levels. Title matches can rank higher than description matches. |
| 78 | + |
| 79 | +```js |
| 80 | +const fuse = new Fuse(docs, { |
| 81 | + keys: [ |
| 82 | + { name: 'title', weight: 2 }, |
| 83 | + { name: 'description', weight: 1 } |
| 84 | + ] |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +### Extended Search |
| 89 | + |
| 90 | +Use operators for precise control: exact match (`=`), prefix (`^`), suffix (`!`), and more. Enable with `useExtendedSearch: true`. |
| 91 | + |
| 92 | +```js |
| 93 | +const fuse = new Fuse(list, { |
| 94 | + useExtendedSearch: true, |
| 95 | + keys: ['title'] |
| 96 | +}) |
11 | 97 |
|
12 | | -Through contributions, donations, and sponsorship, you allow Fuse.js to thrive. Also, you will be recognized as a beacon of support to open-source developers. |
13 | | - |
14 | | -- [Become a backer or sponsor on **GitHub**.](https://github.com/sponsors/krisk) |
15 | | -- [Become a backer or sponsor on **Patreon**.](https://patreon.com/fusejs) |
16 | | -- [One-time donation via **PayPal**.](https://www.paypal.me/kirorisk) |
17 | | - |
18 | | ---- |
19 | | - |
20 | | -<h3 align="center">Sponsors</h3> |
21 | | -<table> |
22 | | -<tbody> |
23 | | - <tr> |
24 | | - <td align="center" valign="middle"> |
25 | | - <a href="https://www.worksome.com" target="_blank"> |
26 | | - <img width="222px" src="https://raw.githubusercontent.com/krisk/Fuse/7a0d77d85ac90063575613b6a738f418b624357f/docs/.vuepress/public/assets/img/sponsors/worksome.svg"> |
27 | | - </a> |
28 | | - </td> |
29 | | - <td align="center" valign="middle"> |
30 | | - <a href="https://www.bairesdev.com/sponsoring-open-source-projects/" target="_blank"> |
31 | | - <img width="222px" src="https://github.com/krisk/Fuse/blob/gh-pages/assets/img/sponsors/bairesdev.png?raw=true"> |
32 | | - </a> |
33 | | - </td> |
34 | | - <td align="center" valign="middle"> |
35 | | - <a href="https://litslink.com/" target="_blank"> |
36 | | - <img width="222px" src="https://github.com/krisk/Fuse/blob/gh-pages/assets/img/sponsors/litslink.svg?raw=true"> |
37 | | - </a> |
38 | | - </td> |
39 | | - </tr> |
40 | | -</body> |
41 | | -</table> |
42 | | - |
43 | | ---- |
44 | | - |
45 | | -## Introduction |
46 | | - |
47 | | -Fuse.js is a lightweight fuzzy-search library, written in TypeScript, with zero dependencies. |
| 98 | +fuse.search('=exact match') // exact match |
| 99 | +fuse.search('^prefix') // starts with |
| 100 | +fuse.search('!term') // does not include |
| 101 | +``` |
| 102 | + |
| 103 | +### Token Search |
| 104 | + |
| 105 | +Splits multi-word queries into individual terms, fuzzy-matches each independently, and ranks results using BM25-style IDF weighting. Enable with `useTokenSearch: true`. |
| 106 | + |
| 107 | +```js |
| 108 | +const fuse = new Fuse(docs, { |
| 109 | + useTokenSearch: true, |
| 110 | + keys: ['title', 'body'] |
| 111 | +}) |
| 112 | + |
| 113 | +fuse.search('express midleware rout') |
| 114 | +// Finds "Express Middleware" and "Express Routing Guide" despite typos |
| 115 | +``` |
| 116 | + |
| 117 | +- **Typo tolerance per word** — each term is fuzzy-matched independently |
| 118 | +- **Relevance ranking** — rare terms are weighted higher than common ones |
| 119 | +- **Word order independent** — `"patterns javascript"` and `"javascript patterns"` return identical results |
| 120 | +- **No query length limit** — long multi-word queries work naturally since each term is searched separately |
| 121 | + |
| 122 | +Available in the full build. See [TOKEN_SEARCH.md](TOKEN_SEARCH.md) for details and performance benchmarks. |
| 123 | + |
| 124 | +### Logical Search |
| 125 | + |
| 126 | +Combine conditions with `$and` and `$or` for complex queries. Available in the full build. |
| 127 | + |
| 128 | +```js |
| 129 | +fuse.search({ |
| 130 | + $and: [ |
| 131 | + { title: 'javascript' }, |
| 132 | + { author: 'crockford' } |
| 133 | + ] |
| 134 | +}) |
| 135 | +``` |
| 136 | + |
| 137 | +### Match Highlighting |
| 138 | + |
| 139 | +Get character-level match indices for highlighting search results in your UI. |
| 140 | + |
| 141 | +```js |
| 142 | +const fuse = new Fuse(list, { |
| 143 | + includeMatches: true, |
| 144 | + keys: ['title'] |
| 145 | +}) |
| 146 | + |
| 147 | +const result = fuse.search('javscript') |
| 148 | +// result[0].matches[0].indices → [[0, 9]] |
| 149 | +``` |
| 150 | + |
| 151 | +### Single String Matching |
| 152 | + |
| 153 | +Use `Fuse.match()` to fuzzy-match a pattern against a single string without creating an index. Useful for one-off comparisons or custom filtering. |
| 154 | + |
| 155 | +```js |
| 156 | +const result = Fuse.match('javscript', 'JavaScript: The Good Parts') |
| 157 | +// → { isMatch: true, score: 0.04, indices: [[0, 9]] } |
| 158 | +``` |
| 159 | + |
| 160 | +### Dynamic Collections |
| 161 | + |
| 162 | +Add and remove documents from a live index without rebuilding. |
| 163 | + |
| 164 | +```js |
| 165 | +fuse.add({ title: 'New Book', author: 'New Author' }) |
| 166 | +fuse.remove((doc) => doc.title === 'Old Book') |
| 167 | +``` |
| 168 | + |
| 169 | +## Builds |
| 170 | + |
| 171 | +Fuse.js ships in two variants: |
| 172 | + |
| 173 | +| Build | Includes | Min + gzip | |
| 174 | +|---|---|---| |
| 175 | +| **Full** | Fuzzy + Extended + Logical + Token search | ~8 kB | |
| 176 | +| **Basic** | Fuzzy search only | ~6.5 kB | |
| 177 | + |
| 178 | +Use the basic build if you only need fuzzy search and want the smallest bundle size. |
48 | 179 |
|
49 | 180 | ## Documentation |
50 | 181 |
|
51 | | -To check out a [live demo](https://fusejs.io/demo.html) and docs, visit [fusejs.io](https://fusejs.io). |
| 182 | +For the full API reference, configuration options, scoring theory, and interactive demos, visit **[fusejs.io](https://fusejs.io)**. |
| 183 | + |
| 184 | +## Supporting Fuse.js |
| 185 | + |
| 186 | +- [Become a backer or sponsor on **GitHub**](https://github.com/sponsors/krisk) |
| 187 | +- [Become a backer or sponsor on **Patreon**](https://patreon.com/fusejs) |
| 188 | +- [One-time donation via **PayPal**](https://www.paypal.me/kirorisk) |
52 | 189 |
|
53 | 190 | ## Develop |
54 | 191 |
|
|
0 commit comments