Fuzzy Search Made Easy with Fuse.js
Ever built an application where users need to find specific items from a large list? Standard searches are great, but what happens when a user misspells something, or uses a slightly different variation of a term? That’s where fuzzy search comes in.
Fuzzy search, also known as approximate string matching, allows users to find relevant results even with typos or variations in their query. It’s a fantastic way to improve user experience and make your application more forgiving. Today, we’re going to look at how to implement this using a popular JavaScript library: Fuse.js.
What is Fuse.js?
Fuse.js is a lightweight, powerful, and flexible fuzzy-searching library. It’s written in JavaScript and can be used in both browser and Node.js environments. It uses a sophisticated algorithm to calculate the similarity between a search query and items in a list, returning the best matches.
Getting Started
First things first, let’s get Fuse.js into our project. If you’re using npm or yarn, it’s as simple as:
npm install fuse.js# oryarn add fuse.jsIf you prefer not to use a package manager, you can include it via a CDN.
Basic Implementation
Let’s imagine we have a list of programming languages and we want to be able to search through them, even if the user makes a mistake.
Here’s our data:
const languages = [ { id: 1, name: "JavaScript", category: "Web" }, { id: 2, name: "Python", category: "Backend" }, { id: 3, name: "Java", category: "Enterprise" }, { id: 4, name: "TypeScript", category: "Web" }, { id: 5, name: "Ruby", category: "Backend" }, { id: 6, name: "C++", category: "Systems" }, { id: 7, name: "Go", category: "Backend" }, { id: 8, name: "Swift", category: "Mobile" }, { id: 9, name: "Kotlin", category: "Mobile" }, { id: 10, name: "PHP", category: "Web" }];Now, let’s initialize Fuse.js with this data. We need to tell Fuse.js which properties of our objects it should search through. This is done using the keys option.
import Fuse from 'fuse.js';
const options = { keys: ['name', 'category'] // Properties to search within};
const fuse = new Fuse(languages, options);With fuse initialized, we can now perform searches. Let’s say a user types "Javascrpt" (a common typo for “JavaScript”).
const searchResults = fuse.search('Javascrpt');
console.log(searchResults);If you run this, you’ll see an array containing the result for “JavaScript” because Fuse.js intelligently matches the slightly misspelled query. The output will look something like this:
[ { "item": { "id": 1, "name": "JavaScript", "category": "Web" }, "refIndex": 0 }]The item property holds the original object that matched, and refIndex is its original index in the languages array.
Advanced Options
Fuse.js offers a lot of configuration for fine-tuning your search. Some useful options include:
includeScore: If set totrue, each result will include ascoreproperty indicating how well it matched (lower is better).threshold: A value between 0 and 1. A lower threshold means fewer results but higher accuracy. The default is0.6.distance: The maximum distance between the search term and the matched characters. Defaults to100.ignoreLocation: Iftrue, Fuse.js will ignore the location of the match within the string. Defaults tofalse.findAllMatches: Iftrue, returns all matches rather than just the best one. Defaults tofalse.
Let’s try an example with scores:
const advancedOptions = { keys: ['name', 'category'], includeScore: true, threshold: 0.4 // More strict matching};
const advancedFuse = new Fuse(languages, advancedOptions);
const advancedResults = advancedFuse.search('Pythn'); // Typo for Python
console.log(advancedResults);This would give you results with their corresponding scores, allowing you to sort or filter further if needed.
Why Use Fuzzy Search?
In short, it makes your users happier. They don’t have to be perfect typists. It reduces frustration and helps them find what they’re looking for faster, even if they aren’t entirely sure how to spell it. For applications with extensive datasets, like e-commerce sites, documentation platforms, or even large configuration files, fuzzy search is not just a nice-to-have, it’s often a necessity.
Fuse.js makes implementing this powerful feature remarkably straightforward. Give it a try in your next project!