JSPM

  • Created
  • Published
  • Downloads 455895
  • Score
    100M100P100Q183424F
  • License Apache-2.0

Next-Generation full text search library with zero dependencies.

Package Exports

  • flexsearch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (flexsearch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme


Search Library

World's fastest and most memory efficient full text search library with zero dependencies.

When it comes to raw search speed FlexSearch outperforms every single searching library out there and also provides flexible search capabilities like multi-word matching, phonetic transformations or partial matching. It also has the most memory-efficient index. Keep in mind that updating / removing existing items from the index has a significant cost. When your index needs to be updated continuously then BulkSearch may be a better choice. FlexSearch also provides you a non-blocking asynchronous processing model as well as web workers to perform any updates or queries on the index in parallel through dedicated balanced threads.

Installation Guide  •  API Reference  •  Example Options  •  Custom Builds

Comparison:

Supported Platforms:

  • Browser
  • Node.js

All Features:

  • Web-Worker Support (not available in Node.js)
  • Contextual Indexes
  • Partial Matching
  • Multiple Words
  • Phonetic Search
  • Relevance-based Scoring
  • Auto-Balanced Cache by Popularity
  • Limit Results
  • Supports Caching
  • Asynchronous Processing
  • Customizable: Matcher, Encoder, Tokenizer, Stemmer, Filter

These features are not available in the 50% smaller light version:

  • WebWorker
  • Asynchronous
  • Cache
  • Built-in encoders except 'balance' and 'icase' (you can still pass in customs)
  • Built-in stemmer and filter (you can still pass in customs)
  • Debug logging
  • index.info() method

The light version is just available as compiled version (flexsearch.light.js).

You can also make Custom Builds pretty simple

FlexSearch introduce a new scoring mechanism called Contextual Search which was invented by Thomas Wilkerling, the author of this library. A Contextual Search incredibly boost up queries to a complete new level. The basic idea of this concept is to limit relevance by its context instead of calculating relevance through the whole (unlimited) distance. Imagine you add a text block of some sentences to an index ID. Assuming the query includes a combination of first and last word from this text block, are they really relevant to each other? In this way contextual search also improves the results of relevance-based queries on large amount of text data.

Note: This feature is actually not enabled by default.

Web-Worker Support

Workers get its own dedicated memory. Especially for larger indexes, web worker improves speed and available memory a lot. FlexSearch index was tested with a 250 Mb text file including 10 Million words. The indexing was done silently in background by multiple parallel running workers in about 7 minutes. The final index reserves ~ 8.2 Mb memory/space. The search result took ~ 0.25 ms.

Note: It is slightly faster to use no web worker when the index or query isn't too big (index < 500,000 words, query < 25 words).

Installation

HTML / Javascript
<html>
<head>
    <script src="js/flexsearch.min.js"></script>
</head>
...

Note: Use flexsearch.min.js for production and flexsearch.js for development.

Use latest from CDN:

<script src="https://cdn.rawgit.com/nextapps-de/flexsearch/master/flexsearch.min.js"></script>
Node.js
npm install flexsearch

In your code include as follows:

var FlexSearch = require("flexsearch");

Or pass in options when requiring:

var index = require("flexsearch").create({/* options */});

AMD

var FlexSearch = require("./flexsearch.js");

API Overview

Global methods:

Index methods:

Usage

Create a new index

FlexSearch.create(<options>)

var index = new FlexSearch();

alternatively you can also use:

var index = FlexSearch.create();
Create a new index and choosing one of the built-in profiles
var index = new FlexSearch("speed");
Create a new index with custom options
var index = new FlexSearch({

    // default values:

    profile: "balance",
    encode: "icase",
    mode: "ngram",
    async: false,
    cache: false
});

Read more about custom options

Add items to an index

Index.add_(id, string)

index.add(10025, "John Doe");

Search items

Index.search(string|options, <limit>, <callback>)

index.search("John");

Limit the result:

index.search("John", 10);

Perform queries asynchronously:

index.search("John", function(result){
    
    // array of results
});

Update item to the index

Index.update(id, string)

index.update(10025, "Road Runner");

Remove item to the index

Index.remove(id)

index.remove(10025);

Reset index

index.reset();

Destroy the index

index.destroy();

Re-Initialize index

Index.init(<options>)

Note: Re-initialization will also destroy the old index!

Initialize (with same options):

index.init();

Initialize with new options:

index.init({

    /* options */
});

Add custom matcher

FlexSearch.addMatcher({REGEX: REPLACE})

Add global matchers for all instances:

FlexSearch.addMatcher({

    'ä': 'a', // replaces all 'ä' to 'a'
    'ó': 'o',
    '[ûúù]': 'u' // replaces multiple
});

Add private matchers for a specific instance:

index.addMatcher({

    'ä': 'a', // replaces all 'ä' to 'a'
    'ó': 'o',
    '[ûúù]': 'u' // replaces multiple
});

Add custom encoder

Define a private custom encoder during creation/initialization:

var index = new FlexSearch({

    encode: function(str){
    
        // do something with str ...
        
        return str;
    }
});

Register a global encoder to be used by all instances

FlexSearch.register(name, encoder)

FlexSearch.register('whitespace', function(str){

    return str.replace(/ /g, '');
});

Use global encoders:

var index = new FlexSearch({ encode: 'whitespace' });

Call encoders directly

Private encoder:

var encoded = index.encode("sample text");

Global encoder:

var encoded = FlexSearch.encode("whitespace", "sample text");
Mixup/Extend multiple encoders
FlexSearch.register('mixed', function(str){
  
    str = this.encode("icase", str);  // built-in
    str = this.encode("whitespace", str); // custom
    
    return str;
});
FlexSearch.register('extended', function(str){
  
    str = this.encode("custom", str);
    
    // do something additional with str ...

    return str;
});

Add custom tokenizer

Define a private custom tokenizer during creation/initialization:

var index = new FlexSearch({

    mode: function(str){
    
        // split string into components, e.g.:
        
        return str.split(/ -\//g);
    }
});

Get info

index.info();

Returns information about the index, e.g.:

{
    "bytes": 3600356288,
    "id": 0,
    "matchers": 0,
    "size": 10000,
    "status": false
}

Chaining

Simply chain methods like:

var index = FlexSearch.create()
                      .addMatcher({'â': 'a'})
                      .add(0, 'foo')
                      .add(1, 'bar');
index.remove(0).update(1, 'foo').add(2, 'foobar');

Enable Contextual Index

Create context-enabled index and also set the limit of relevance (depth):

var index = new FlexSearch({

    encode: "icase",
    mode: "strict",
    depth: 3
});

Use WebWorker (Browser only)

Create worker-enabled index and also set the count of parallel threads:

var index = new FlexSearch({

    encode: "icase",
    mode: "full",
    async: true,
    worker: 4
});

Adding items to worker index as usual (async enabled):

index.add(10025, "John Doe");

Perform search and simply pass in callback like:

index.search("John Doe", function(results){

    // do something with array of results
});

Options

FlexSearch ist highly customizable. Make use of the the right options can really improve your results as well as memory economy or query time.

Option Values Description
profile





"memory"
"speed"
"match"
"score"
"balance"
"fastest"
The configuration profile. Choose your preferation.
mode





"strict"
"foward"
"reverse"
"ngram"
"full"
function()
The indexing mode (tokenizer).

Choose one of the built-ins or pass a custom tokenizer function.
encode






false
"icase"
"simple"
"advanced"
"extra"
"balance"
function()
The encoding type.

Choose one of the built-ins or pass a custom encoding function.
cache


false
true
{number}
Enable/Disable and/or set capacity of cached entries.

When passing a number as a limit the cache automatically balance stored entries related to their popularity.

Note: When just using "true" the cache has no limits and is actually 5 times faster (the balancer should not run).
async

true
false
Enable/Disable asynchronous processing.

Each job will be queued for non-blocking processing. Recommended when using WebWorkers.
worker

false
{number}
Enable/Disable and set count of running worker threads.
depth

false
{number}
Enable/Disable contextual indexing and also sets relevance depth (experimental).
threshold

false
{number}
Enable/Disable the threshold of minimum relevance results should have.

Note: You can take a lower threshold for indexing and pass a higher value when calling .search(), but not other turn around.
stemmer

false
{function}
Disable or pass in custom object/array.
filter

false
{function}
Disable or pass in custom object/array.

Tokenizer

Tokenizer effects the required memory also as query time and flexibility of partial matches. Try to choose the most upper of these tokenizer which fits your needs:

Option Description Example Memory Factor (n = length of word)
"strict" index whole words foobar * 1
"ngram" (default) index words partially through phonetic n-grams foobar
foobar
* n / 3
"foward" incrementally index words in forward direction foobar
foobar
* n
"reverse" incrementally index words in both directions foobar
foobar
* 2n - 1
"full" index every possible combination foobar
foobar
* n * (n - 1)

Phonetic Encoding

Encoding effects the required memory also as query time and phonetic matches. Try to choose the most upper of these encoders which fits your needs, or pass in a custom encoder:

Option Description False-Positives Compression
false Turn off encoding no no
"icase" (default) Case in-sensitive encoding no no
"simple" Phonetic normalizations no ~ 7%
"advanced" Phonetic normalizations + Literal transformations no ~ 35%
"extra" Phonetic normalizations + Soundex transformations yes ~ 60%
function() Pass custom encoding: function(string):string

Comparison (Matches)

Reference String: "Björn-Phillipp Mayer"

Query iCase Simple Advanced Extra
björn yes yes yes yes
björ yes yes yes yes
bjorn no yes yes yes
bjoern no no yes yes
philipp no no yes yes
filip no no yes yes
björnphillip no yes yes yes
meier no no yes yes
björn meier no no yes yes
meier fhilip no no yes yes
byorn mair no no no yes
(false positives) no no no yes

Memory Usage

The required memory for the index depends on several options:

Encoding Memory usage of every ~ 100,000 indexed word
false 260 kb
"icase" (default) 210 kb
"simple" 190 kb
"advanced" 150 kb
"extra" 90 kb
Mode Multiplied with: (n = average length of indexed words)
"strict" * 1
"ngram" (default) * n / 3
"forward" * n
"reverse" * 2n - 1
"full" * n * (n - 1)
Contextual Index Multiply the sum above with:
* (depth * 2 + 1)

Compare Memory Consumption

The book "Gulliver's Travels" (Swift Jonathan 1726) was used for this test.


Built-in Profiles

You can pass a built-in profile during creation/initialization. They have these following settings:

Standard profile: "default"

{
    encode: "icase",
    mode: "forward"
}

Memory-optimized profile: "memory"

{
    encode: "extra",
    mode: "strict",
    threshold: 7,
    stemmer: true,
    filter: true
}

Speed-optimized profile: "speed"

{
    encode: "icase",
    mode: "strict",
    threshold: 7,
    depth: 2
}

Matching-tolerant profile: "match"

{
    encode: "extra",
    mode: "full"
}

Relevance-optimized profile: "score"

{
    encode: "extra",
    mode: "strict",
    threshold: 5,
    depth: 4
}

Most-balanced profile: "balanced"

{
    encode: "balanced",
    mode: "ngram",
    threshold: 6,
    depth: 3
}

Absolute fastest profile: "fastest"

{
    encode: "icase",
    threshold: 9,
    depth: 1
}

Compare these options above:

Custom Builds

Default Build:

npm run build

Light Build:

npm run build-light

Custom Build:

npm run build-custom SUPPORT_WORKER=true SUPPORT_ASYNC=true

Supported flags:

  • SUPPORT_DEBUG
  • SUPPORT_WORKER
  • SUPPORT_CACHE
  • SUPPORT_ASYNC
  • SUPPORT_BUILTINS (english stemmer and filter)

Alternatively you can also use:

node compile SUPPORT_WORKER=true

The custom build was saved to flexsearch.custom.js


Copyright 2018 Thomas Wilkerling
Released under the Apache 2.0 License