02/08/2023

Svelte Full Text Search - flexsearch

"flexsearch" is a JavaScript library for fast, flexible, and robust search and indexing. It provides a high-performance search engine that is optimized for search and indexing, and can be used in both client-side and server-side applications. It supports a range of features including full-text search, fuzzy search, query highlighting, and auto-suggestions. Here's how to implement it into a Svelte project.

Install

npm install flexsearch

Importing into your project

flexsearch has three types of indexes:

  • Index is a flat high performance index that stores id-content-pairs.
  • Worker / WorkerIndex is a flat index that stores id-content pairs but runs in the background as a dedicated worker thread.
  • Document is a multi-field index that can store complex JSON documents (could also exist of worker indexes).

Most of you probably need just one of them according to your scenario. To import via the npm package you will need to import just the single index you want below is an example of Index and Document being imported into a project:

import Document from 'flexsearch/src/document';
import Index from 'flexsearch/src/index';

NOTE: if you are importing into Svelte you will need to edit the package.json of the module and add "type": "module" or Svelte will not allow it to be imported

Using it in Svelte

Adding the documents

new Document will create a flexsearch document index that allows you to add your documents to the index. It takes a document descriptor which contains a document object with the index field being the key of the object you want indexed.

let findDocument = new Document({
    document: {
        index: ['title', 'description', 'content']
    }
});
for (let i = 0; i < data.docs.length; i++) {
    findDocument.add(docs[i]);
}

Search functions

To search the document index use the index you created, in this case, findDocument. Use the .search function and pass your search query to the function. This will return an object with the keys being the same as the index field of the document you passed upon initiation. The keys will contain arrays of the ids of the documents that match the search query. Here I am flattening the object into an array of IDs and then filtering them using svelte in the {#each } loop only if there is a search query.

let search = "";
let results = [];
function findDocuments() {
    results = findDocument
        .search(search)
        .map((e) => e.result)
        .flat();
}

Svelte elements

<input
    type="search"
    id="search"
    placeholder="Search"
    bind:value={search}
    on:keydown={findDocuments}
/>

// Filter the documents if the search term is not blank
{#each docs as doc}
    {#if search == '' || results.indexOf(doc.id) != -1}
        // Search Results
    {/if}
{/each}
Back

Comments


Be the first to comment!

Read More

Node JS Logo
Node JS Logo

Setting up Node.JS for production

Node.js is a popular JavaScript runtime that allows developers to build server-side applications with JavaScript. PM2 is a process manager for Node.js applications that helps to keep your applications running smoothly and automatically restart th...

Svelte Full Text Search - flexsearch

"flexsearch" is a JavaScript library for fast, flexible, and robust search and indexing. It provides a high-performance search engine that is optimized for search and indexing, and can be used in both client-side and server-side applications. It supports a range of features including full-text search, fuzzy...

JS Chunk Array

Here's a simple function that uses generators to chunk an array. Generators are a type of function that returns an iterator and traverse it one at a time if you u...

Seeded Random Number Generator in JS

Random numbers are an essential tool for developers, whether it's for generating random colors for page elements, creating dynamic content on canvas, or any other purpose. However, one issue with the default Math.random() function is you can not repeat the results. T...