Convert Figma logo to code with AI

bvaughn logojs-search

JS Search is an efficient, client-side search library for JavaScript and JSON objects

2,201
115
2,201
8

Top Related Projects

18,051

Lightweight fuzzy-search, in JavaScript

Tiny and powerful JavaScript full-text search engine for browser and Node

Next-Generation full text search library for Browser and Node.js

8,902

A bit like Solr, but much smaller and not as bright

A persistent, network resilient, full text search library for the browser and Node.js

Based on lunr.js, but more flexible and customized.

Quick Overview

js-search is a lightweight, client-side JavaScript library for creating fast and efficient search functionality. It allows for in-memory searching of JavaScript objects, making it ideal for small to medium-sized datasets that can be loaded into the browser's memory.

Pros

  • Fast and efficient for client-side searching
  • Supports multiple search strategies (prefix, substring, exact match)
  • Customizable indexing and tokenization
  • No server-side dependencies, reducing backend complexity

Cons

  • Limited to datasets that can fit in browser memory
  • May not be suitable for very large datasets or server-side searching
  • Lacks advanced features like fuzzy matching or relevance scoring
  • Not actively maintained (last update was in 2019)

Code Examples

  1. Basic search setup:
import * as JsSearch from 'js-search';

const search = new JsSearch.Search('id');
search.addIndex('name');
search.addIndex('description');
search.addDocuments(myDataArray);

const results = search.search('query');
  1. Custom tokenizer:
import * as JsSearch from 'js-search';

const search = new JsSearch.Search('id');
search.tokenizer = new JsSearch.StemmingTokenizer(
  new JsSearch.SimpleTokenizer(),
  new JsSearch.EnglishStemmer()
);
  1. Prefix search strategy:
import * as JsSearch from 'js-search';

const search = new JsSearch.Search('id');
search.indexStrategy = new JsSearch.PrefixIndexStrategy();
search.searchIndex = new JsSearch.TfIdfSearchIndex('id');

Getting Started

To use js-search in your project:

  1. Install the package:

    npm install js-search
    
  2. Import and set up a basic search:

    import * as JsSearch from 'js-search';
    
    const search = new JsSearch.Search('id');
    search.addIndex('field1');
    search.addIndex('field2');
    search.addDocuments(yourDataArray);
    
    const results = search.search('your search query');
    console.log(results);
    
  3. Customize the search as needed using different indexing strategies, tokenizers, or search indexes.

Competitor Comparisons

18,051

Lightweight fuzzy-search, in JavaScript

Pros of Fuse

  • Supports fuzzy searching, allowing for more flexible and forgiving search results
  • Offers customizable scoring and weighting options for fine-tuning search relevance
  • Provides a more extensive API with additional features like highlighting matches

Cons of Fuse

  • Generally slower performance compared to js-search, especially for large datasets
  • Higher memory usage due to its more complex matching algorithms
  • Steeper learning curve for advanced configurations and optimizations

Code Comparison

js-search:

const search = new JsSearch.Search('id');
search.addIndex('title');
search.addIndex('author');
search.addDocuments(books);
const results = search.search('javascript');

Fuse:

const fuse = new Fuse(books, {
  keys: ['title', 'author'],
  threshold: 0.3
});
const results = fuse.search('javascript');

Both libraries offer simple APIs for basic searching, but Fuse provides more options for customization in its initialization. js-search focuses on exact matching and is generally faster, while Fuse offers fuzzy matching capabilities at the cost of performance. The choice between the two depends on the specific requirements of your project, such as dataset size, search accuracy needs, and performance constraints.

Tiny and powerful JavaScript full-text search engine for browser and Node

Pros of MiniSearch

  • Supports fuzzy search and prefix search out of the box
  • Offers a more flexible API for customizing search behavior
  • Provides built-in tokenization and term processing options

Cons of MiniSearch

  • Slightly larger bundle size compared to js-search
  • May have a steeper learning curve for beginners due to more advanced features

Code Comparison

MiniSearch:

const miniSearch = new MiniSearch({
  fields: ['title', 'text'],
  storeFields: ['title', 'category']
})
miniSearch.addAll(documents)
const results = miniSearch.search('query')

js-search:

const search = new JsSearch.Search('id')
search.addIndex('title')
search.addIndex('text')
search.addDocuments(documents)
const results = search.search('query')

Key Differences

  • MiniSearch offers more advanced search features like fuzzy matching and prefix search
  • js-search has a simpler API, which may be easier for basic use cases
  • MiniSearch provides more control over indexing and searching behavior
  • js-search has a smaller footprint and may be faster for simple searches
  • MiniSearch supports field boosting and result ranking out of the box

Both libraries are suitable for client-side search, but MiniSearch is more feature-rich, while js-search focuses on simplicity and performance for basic search scenarios.

Next-Generation full text search library for Browser and Node.js

Pros of FlexSearch

  • Significantly faster performance, especially for large datasets
  • More flexible and customizable, with support for various indexing methods
  • Smaller bundle size, making it more suitable for browser-based applications

Cons of FlexSearch

  • Steeper learning curve due to more complex API and configuration options
  • Less straightforward implementation for simple use cases
  • May require more setup and fine-tuning to achieve optimal results

Code Comparison

FlexSearch:

const index = new FlexSearch({
  encode: "icase",
  tokenize: "forward",
  threshold: 0
});
index.add(1, "John Doe");
const results = index.search("john");

js-search:

const search = new JsSearch.Search('id');
search.addIndex('name');
search.addDocuments([{id: 1, name: "John Doe"}]);
const results = search.search("john");

Both libraries offer full-text search capabilities, but FlexSearch provides more advanced features and better performance at the cost of increased complexity. js-search is simpler to use and may be sufficient for smaller datasets or less demanding applications. The choice between the two depends on the specific requirements of your project, including performance needs, dataset size, and desired customization options.

8,902

A bit like Solr, but much smaller and not as bright

Pros of Lunr.js

  • More advanced search features, including fuzzy matching and boosting
  • Built-in stemming support for multiple languages
  • Smaller file size and faster performance for large datasets

Cons of Lunr.js

  • Steeper learning curve due to more complex API
  • Less flexibility in customizing search behavior
  • May be overkill for simple search requirements

Code Comparison

Lunr.js:

var idx = lunr(function () {
  this.field('title')
  this.field('body')
  this.add({
    "title": "Lunr",
    "body": "Like Solr, but much smaller, and not as bright."
  })
})

js-search:

var search = new JsSearch.Search('id');
search.addIndex('title');
search.addIndex('body');
search.addDocuments([
  { id: 1, title: 'JsSearch', body: 'Lightweight search library for JavaScript.' }
]);

Both libraries offer full-text search capabilities for JavaScript applications, but they differ in complexity and feature set. Lunr.js provides more advanced search functionality at the cost of a steeper learning curve, while js-search offers a simpler API with more straightforward customization options. The choice between the two depends on the specific requirements of your project, such as the need for advanced search features, performance considerations, and the complexity of the data being searched.

A persistent, network resilient, full text search library for the browser and Node.js

Pros of search-index

  • More advanced features like faceted search and range queries
  • Better scalability for larger datasets
  • Supports both in-memory and persistent storage options

Cons of search-index

  • Steeper learning curve due to more complex API
  • Larger bundle size, which may impact performance in browser environments
  • Less suitable for simple, small-scale search scenarios

Code Comparison

search-index:

const si = require('search-index')
const { index, search } = await si()
await index([
  { id: '1', text: 'Hello world' },
  { id: '2', text: 'Hello JavaScript' }
])
const results = await search('hello')

js-search:

const JsSearch = require('js-search')
const search = new JsSearch.Search('id')
search.addIndex('text')
search.addDocuments([
  { id: '1', text: 'Hello world' },
  { id: '2', text: 'Hello JavaScript' }
])
const results = search.search('hello')

Summary

search-index is more powerful and scalable, suitable for complex search requirements and larger datasets. It offers advanced features but comes with a steeper learning curve and larger bundle size. js-search, on the other hand, is simpler to use and more lightweight, making it ideal for basic search functionality in smaller applications or client-side implementations. The choice between the two depends on the specific needs of your project, considering factors like dataset size, required features, and performance constraints.

Based on lunr.js, but more flexible and customized.

Pros of elasticlunr.js

  • More advanced search capabilities, including fuzzy search and field-specific boosting
  • Supports incremental indexing, allowing for efficient updates to the search index
  • Smaller file size and faster performance for large datasets

Cons of elasticlunr.js

  • More complex setup and configuration compared to js-search
  • May be overkill for simple search requirements
  • Less actively maintained, with fewer recent updates

Code Comparison

elasticlunr.js:

var index = elasticlunr(function () {
  this.addField('title');
  this.addField('body');
  this.setRef('id');
});
index.addDoc({id: 1, title: 'Example', body: 'Some content'});
var results = index.search("example");

js-search:

var search = new JsSearch.Search('id');
search.addIndex('title');
search.addIndex('body');
search.addDocuments([{id: 1, title: 'Example', body: 'Some content'}]);
var results = search.search("example");

Both libraries offer full-text search capabilities for JavaScript applications, but elasticlunr.js provides more advanced features at the cost of increased complexity. js-search is simpler to use and may be sufficient for basic search needs, while elasticlunr.js is better suited for more demanding search requirements and larger datasets.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Installation | Overview | Tokenization | Stemming | Stop Words | Search Index | Index Strategy

Js Search: client-side search library

Js Search enables efficient client-side searches of JavaScript and JSON objects. It is ES5 compatible and does not require jQuery or any other third-party libraries.

Js Search began as a lightweight implementation of Lunr JS, offering runtime performance improvements and a smaller file size. It has since expanded to include a rich feature set- supporting stemming, stop-words, and TF-IDF ranking.

Here are some JS Perf benchmarks comparing the two search libraries. (Thanks to olivernn for tweaking the Lunr side for a better comparison!)

If you're looking for a simpler, web-worker optimized JS search utility check out js-worker-search.


If you like this project, 🎉 become a sponsor or ☕ buy me a coffee


Installation

You can install using either Bower or NPM like so:

npm install js-search
bower install js-search

Overview

At a high level you configure Js Search by telling it which fields it should index for searching and then add the objects to be searched.

For example, a simple use of JS Search would be as follows:

import * as JsSearch from 'js-search';

var theGreatGatsby = {
  isbn: '9781597226769',
  title: 'The Great Gatsby',
  author: {
    name: 'F. Scott Fitzgerald'
  },
  tags: ['book', 'inspirational']
};
var theDaVinciCode = {
  isbn: '0307474275',
  title: 'The DaVinci Code',
  author: {
    name: 'Dan Brown'
  },
  tags: ['book', 'mystery']
};
var angelsAndDemons = {
  isbn: '074349346X',
  title: 'Angels & Demons',
  author: {
    name: 'Dan Brown',
  },
  tags: ['book', 'mystery']
};

var search = new JsSearch.Search('isbn');
search.addIndex('title');
search.addIndex(['author', 'name']);
search.addIndex('tags')

search.addDocuments([theGreatGatsby, theDaVinciCode, angelsAndDemons]);

search.search('The');    // [theGreatGatsby, theDaVinciCode]
search.search('scott');  // [theGreatGatsby]
search.search('dan');    // [angelsAndDemons, theDaVinciCode]
search.search('mystery') // [angelsAndDemons, theDaVinciCode]

Tokenization

Tokenization is the process of breaking text (e.g. sentences) into smaller, searchable tokens (e.g. words or parts of words). Js Search provides a basic tokenizer that should work well for English but you can provide your own like so:

search.tokenizer = {
  tokenize( text /* string */ ) {
    // Convert text to an Array of strings and return the Array
  }
};

Stemming

Stemming is the process of reducing search tokens to their root (or "stem") so that searches for different forms of a word will still yield results. For example "search", "searching" and "searched" can all be reduced to the stem "search".

Js Search does not implement its own stemming library but it does support stemming through the use of third-party libraries.

To enable stemming, use the StemmingTokenizer like so:

var stemmer = require('porter-stemmer').stemmer;

search.tokenizer =
	new JsSearch.StemmingTokenizer(
        stemmer, // Function should accept a string param and return a string
	    new JsSearch.SimpleTokenizer());

Stop Words

Stop words are very common (e.g. a, an, and, the, of) and are often not semantically meaningful. By default Js Search does not filter these words, but filtering can be enabled by using the StopWordsTokenizer like so:

search.tokenizer =
	new JsSearch.StopWordsTokenizer(
    	new JsSearch.SimpleTokenizer());

By default Js Search uses a slightly modified version of the Google History stop words listed on www.ranks.nl/stopwords. You can modify this list of stop words by adding or removing values from the JsSearch.StopWordsMap object like so:

JsSearch.StopWordsMap.the = false; // Do not treat "the" as a stop word
JsSearch.StopWordsMap.bob = true;  // Treat "bob" as a stop word

Note that stop words are lower case and so using a case-sensitive sanitizer may prevent some stop words from being removed.

Configuring the search index

There are two search indices packaged with js-search.

Term frequency–inverse document frequency (or TF-IDF) is a numeric statistic intended to reflect how important a word (or words) are to a document within a corpus. The TF-IDF value increases proportionally to the number of times a word appears in the document but is offset by the frequency of the word in the corpus. This helps to adjust for the fact that some words (e.g. and, or, the) appear more frequently than others.

By default Js Search supports TF-IDF ranking but this can be disabled for performance reasons if it is not required. You can specify an alternate ISearchIndex implementation in order to disable TF-IDF, like so:

// default
search.searchIndex = new JsSearch.TfIdfSearchIndex();

// Search index capable of returning results matching a set of tokens
// but without any meaningful rank or order.
search.searchIndex = new JsSearch.UnorderedSearchIndex();

Configuring the index strategy

There are three index strategies packaged with js-search.

PrefixIndexStrategy indexes for prefix searches. (e.g. the term "cat" is indexed as "c", "ca", and "cat" allowing prefix search lookups).

AllSubstringsIndexStrategy indexes for all substrings. In other word "c", "ca", "cat", "a", "at", and "t" all match "cat".

ExactWordIndexStrategy indexes for exact word matches. For example "bob" will match "bob jones" (but "bo" will not).

By default Js Search supports prefix indexing but this is configurable. You can specify an alternate IIndexStrategy implementation in order to disable prefix indexing, like so:

// default
search.indexStrategy = new JsSearch.PrefixIndexStrategy();

// this index strategy is built for all substrings matches.
search.indexStrategy = new JsSearch.AllSubstringsIndexStrategy();

// this index strategy is built for exact word matches.
search.indexStrategy = new JsSearch.ExactWordIndexStrategy();

NPM DownloadsLast 30 Days