DEV Community

Cover image for Best fuzzy search module
shrey vijayvargiya
shrey vijayvargiya

Posted on

Best fuzzy search module

Add accessible search functionalities in front without ElasticSearch and machine learning.

Under the Hood
Fuzzy search is searching abstracts nearby and exact words. For example, “vin” is the searched word that will return the following terms —

  • Edin
  • Vim
  • inside
  • in-depth
  • closing
  • writing
  • blockchain
  • and many more

Fuzzy search becomes essential for your website to add powerful search capabilities beyond the human scope.

Google use machine learning, and ElasticSearch uses an indexing algorithm to add powerful search functionalities for the user.

What if you don’t want to use machine learning and paid versions of ElasticSearch and still want to add fuzzy search to your website?

Fuse.js to the rescue
Fuse.js is a powerful, lightweight fuzzy-search library with zero dependencies.

Why Fuse?
With Fuse.js, you don’t need to set up a dedicated backend to handle the search.
Simplicity and performance were the main criteria when developing this library.
Fuse can be used in the backend, like Node.js or Ruby.
2,886,201 weekly downloads on npm

Getting started
It’s easy to install with just one line of code.

yarn add fuse.js
Enter fullscreen mode Exit fullscreen mode

Easy to import

import Fuse from 'fuse.js.'
Enter fullscreen mode Exit fullscreen mode

CDN import

<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.js"></script>
Enter fullscreen mode Exit fullscreen mode

Live Demo

APIs
Fuse provides tonnes of methods while searching the similar and exact patterns. For example,

  • Case sensitive — Return result matching only cases
  • Score searching — A score of 0indicates a perfect match, while a score of 1 indicates a complete mismatch.
  • Minimum match — Match the minimum characters.
  • Should sort — Give the sorted result
  • Find all matches — Give only the perfect match till the end

Methods

  • Search — Search the entire collection of documents and return the list of search results
  • setCollection — Set/replace the entire collection documents.
  • Add, Remove — Add and remove the doc from the collection.
  • removeAt — Remove the doc from the collection at the specified index.
  • getIndex — Returns the generated Fuse index

Indexing
Pre-generate the index from the list, and pass it directly into the Fuse instance. If the list is (considerably) extensive, it speeds up instantiation.

Gist code

Examples
Once we import the fuse module, provide the collection and value as the parameter.

Codesandbox example

Fuse also provides a Nested search. I am using dot notation, array notion or providing a function to search in the nested array or collection.

Read more here — Nested Search

Conclusion
The overall experience of adding a fuse is good. Easy to install, lightweight, can be used in the backend and easy to implement.

I have added robust search functionality on our website iHateReading using fuse. The experience and outcome are well expected, and I can use it directly with Node.js or backend services in future if required.

I will strongly recommend using a fuse for your next project.

That’s it for today, until next time, have a good day.

Keep developing
Shrey
iHateReading

Top comments (0)