flexsearch tutorial

person holding compass in forest
Reading Time: 2 minutes

What is flexsearch?

FlexSearch is a full-text search library for JavaScript, which can be used in both Node.js and the browser. It is designed to be fast, efficient, and customizable.

In Node.js, FlexSearch can be used to index and search through large amounts of text data. It uses a combination of in-memory data structures and on-disk persistence to allow for efficient indexing and searching of large datasets.

FlexSearch supports a variety of search features, including full-text search, fuzzy search, and exact match search. It also supports boolean queries, phrase search, and proximity search, which allows for more precise and advanced searching.

FlexSearch is easy to use and integrates well with Node.js applications. It is also highly customizable, allowing you to tweak and configure the search algorithms to suit your specific needs.

Overall, FlexSearch is a powerful tool for anyone who needs to perform fast and efficient full-text searches in Node.js applications.


flexsearch It’s a module available through npm or yarn to index in-memory data. With this module, you will be able to search or display related data on your site.

We are going to build a small example to see how it works.

How to install flexsearch

Like any other Node.js module, install it like this:

      
yarn add flexsearch
      
    

Or

      
npm install flexsearch
      
    

How to use flexsearch

Just create an index.js file to see how it works.

      
const { Index } = require("flexsearch");

const options = {
    charset: "latin:extra",
    preset: 'match',
    tokenize: 'strict',
    cache: false
}
const index = new Index(options);

// my collection
const recipes = [
    {id:1, title: 'Orange cake'},
    {id:2, title: 'New York-Style Bacon Egg and Cheese Sandwich'},
    {id:3, title: 'Bacon Wrapped Cheese Stuffed Meatloaf'},
    {id:4, title: 'French Yogurt Cake'},
    {id:5, title: 'Gougeres (French Cheese Puffs)'},
    {id:6, title: 'Authentic Brazilian Cheese Bread (Pão de Queijo)'},
    {id:7, title: 'Camarão na Moranga (Brazilian Shrimp Stuffed Pumpkin)'},
    {id:8, title: 'Parmesan Cheese Muffins'},
    {id:9, title: 'Cookie Dough Stuffed Oreos'},
]

// index my collection
recipes.forEach((recipe) =>{
    index.add(recipe.id, recipe.title)
})


// search (it will return an array of ids)
const ids = index.search('Cookie', 5);
console.debug(ids);
// based on the ids returned by the index, look for the recipes for those ids
const result = recipes.filter((recipe) => ids.includes(recipe.id));
console.debug(result);

Execution output

      
[ 9, 1, 4, 6 ]
[
  { id: 1, title: 'Orange cake' },
  { id: 4, title: 'French Yogurt Cake' },
  { id: 6, title: 'Authentic Brazilian Cheese Bread (Pão de Queijo)' },
  { id: 9, title: 'Cookie Dough Stuffed Oreos' }
]

      
   
, ,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: