Fuse.js - JavaScript fuzzy-search library

Fuse.js

Lightweight fuzzy-search library

View on GitHub

# Live Demo

Step 1: Insert your items


Step 2: Set the options


Indicates whether comparisons should be case sensitive.
Whether the score should be included in the result set. A score of 0 indicates a perfect match, while a score of 1 indicates a complete mismatch.
Whether the matches should be included in the result set. When true, each record in the result set will include the indices of the matched characters: indices: [start, end]. These can consequently be used for highlighting purposes.
When set to include matches, only the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character index returns, set to 2)
Whether to sort the result list, by score.
When true, the algorithm will search individual words and the full string, computing the final score as a function of both. In this case, the threshold, distance, and location are inconsequential for individual tokens, and are thus ignored.
When true, the result set will only include records that match all tokens. Will only work if tokenize is also true.
When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string.
The name of the identifier property. If specified, the returned result will be a list of the items' identifiers, otherwise it will be a list of the items.

List of properties that will be searched. This supports nested properties, weighted search, searching in arrays
Determines approximately where in the text is the pattern expected to be found.
At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything.
Determines how close the match must be to the fuzzy location (specified by location). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match be at the exact location specified, a distance of 1000 would require a perfect match to be within 800 characters of the location to be found using a threshold of 0.8.
The maximum length of the pattern. The longer the pattern (i.e. the search query), the more intensive the search operation will be. Whenever the pattern exceeds the maxPatternLength, an error will be thrown. Why is this important? Read this.

Here's the JavaScript:



Your search results:



# Support Fuse.js Development


# Install

# CDN

Available on jsdelivr or cdnjs (takes some time to sync so the latest version might not be available yet).

# NPM

NPM is the recommended installation method. It pairs nicely with a CommonJS module bundler such as Webpack or Browserify.

# latest stable
$ npm install fuse.js

# Examples

# Searching by ID

var books = [{
  'ISBN': 'A',
  'title': "Old Man's War",
  'author': 'John Scalzi'
}, {
  'ISBN': 'B',
  'title': 'The Lock Artist',
  'author': 'Steve Hamilton'
}]
+
var options = {
  keys: ['title', 'author'],
  id: 'ISBN'
}
var fuse = new Fuse(books, options)

fuse.search('old')
=
[
  "A"
]

var books = [{
  title: "Old Man's War fiction",
  author: 'John X',
  tags: ['war']
}, {
  title: 'Right Ho Jeeves',
  author: 'P.D. Mans',
  tags: ['fiction', 'war']
}]
+
var options = {
  keys: [{
    name: 'title',
    weight: 0.3
  }, {
    name: 'author',
    weight: 0.7
  }]
};
var fuse = new Fuse(books, options)

fuse.search('Man')
=
[{
  "title": "Right Ho Jeeves",
  "author": "P.D. Mans",
  "tags": ["fiction", "war"]
}, {
  "title": "Old Man's War fiction",
  "author": "John X",
  "tags": ["war"]
}]

# Searching in arrays of strings

var books = [{
  'title': "Old Man's War",
  'author': 'John Scalzi',
  'tags': ['fiction']
}, {
  'title': 'The Lock Artist',
  'author': 'Steve',
  'tags': ['thriller']
}]
+
var options = {
  keys: ['author', 'tags']
};
var fuse = new Fuse(books, options)

fuse.search('tion')
=
[{
  "title": "Old Man's War",
  "author": "John Scalzi",
  "tags": ["fiction"]
}]

# Searching in arrays of objects

var books = [{
  'title': "Old Man's War",
  'author': {
    'name': 'John Scalzi',
    'tags': [{
      value: 'American'
    }]
  }
}, {
  'title': 'The Lock Artist',
  'author': {
    'name': 'Steve Hamilton',
    'tags': [{
      value: 'English'
    }]
  }
}]
+
var options = {
  keys: ['author.tags.value'],
};
var fuse = new Fuse(books, options)

fuse.search('engsh')
=
[{
  "title": "The Lock Artist",
  "author": {
    "tags": [{
      "value": "English"
    }]
  }
}]