Skip to main content
Module

x/fuse/docs/examples.md

Lightweight fuzzy-search, in JavaScript
Very Popular
Latest
File

Examples

Search String Array

:::: tabs ::: tab List

const list = ["Old Man's War", 'The Lock Artist']

::: ::: tab JS

const options = {
  includeScore: true
}

const fuse = new Fuse(list, options)

const result = fuse.search('od man')

:::

::: tab Output

[
  {
    "item": "Old Man's War",
    "refIndex": 0,
    "score": 0.35
  }
]

::: ::::

Search Object Array

:::: tabs ::: tab List

const list = [
  {
    title: "Old Man's War",
    author: 'John Scalzi',
    tags: ['fiction']
  },
  {
    title: 'The Lock Artist',
    author: 'Steve',
    tags: ['thriller']
  }
]

::: ::: tab JS

const options = {
  includeScore: true,
  // Search in `author` and in `tags` array
  keys: ['author', 'tags']
}

const fuse = new Fuse(list, options)

const result = fuse.search('tion')

:::

::: tab Output

[
  {
    "item": {
      "title": "Old Man's War",
      "author": "John Scalzi",
      "tags": ["fiction"]
    },
    "refIndex": 0,
    "score": 0.03
  }
]

::: ::::

You can search through nested values by providing the path via dot (.) or array notation.

:::: tabs ::: tab List

const list = [
  {
    title: "Old Man's War",
    author: {
      name: 'John Scalzi',
      tags: [
        {
          value: 'American'
        }
      ]
    }
  },
  {
    title: 'The Lock Artist',
    author: {
      name: 'Steve Hamilton',
      tags: [
        {
          value: 'English'
        }
      ]
    }
  }
]

::: ::: tab JS

const options = {
  includeScore: true,
  // equivalent to `keys: [['author', 'tags', 'value']]`
  keys: ['author.tags.value']
}

const fuse = new Fuse(list, options)

const result = fuse.search('engsh')

:::

::: tab Output

[
  {
    "item": {
      "title": "The Lock Artist",
      "author": {
        "name": "Steve Hamilton",
        "tags": [
          {
            "value": "English"
          }
        ]
      }
    },
    "refIndex": 1,
    "score": 0.4
  }
]

::: ::::

::: warning IMPORTANT The path has to eventually point to a string, otherwise you will not get any results. :::

You can allocate a weight to keys to give them higher (or lower) values in search results. The weight value has to be greater than 0.

:::: tabs ::: tab List

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

::: ::: tab JS

const options = {
  includeScore: true,
  keys: [
    {
      name: 'title',
      weight: 0.3
    },
    {
      name: 'author',
      weight: 0.7
    }
  ]
}

// Create a new instance of Fuse
const fuse = new Fuse(books, options)

// Now search for 'Man'
const result = fuse.search('Man')

:::

::: tab Output

[
  {
    "item": {
      "title": "Right Ho Jeeves",
      "author": "P.D. Mans",
      "tags": ["fiction", "war"]
    },
    "refIndex": 1, // index in the original list
    "score": 0.12282280261157906
  },
  {
    "item": {
      "title": "Old Man's War fiction",
      "author": "John X",
      "tags": ["war"]
    },
    "refIndex": 0,
    "score": 0.3807307877431757
  }
]

::: ::::

Default weight

When a weight isn’t provided, it will default to 1. In the following example, while author has been given a weight of 2, title will be assigned a weight of 1.

const fuse = new Fuse(books, {
  keys: [
    'title', // will be assigned a `weight` of 1
    {
      name: 'author',
      weight: 2
    }
  ]
})

Note that internally Fuse will normalize the weights to be within 0 and 1 exclusive.

This form of advanced searching allows you to fine-tune results.

White space acts as an AND operator, while a single pipe (|) character acts as an OR operator.

Token Match type Description
jscript fuzzy-match Items that fuzzy match jscript
=scheme exact-match Items that are scheme
'python include-match Items that include python
!ruby inverse-exact-match Items that do not include ruby
^java prefix-exact-match Items that start with java
!^earlang inverse-prefix-exact-match Items that do not start with earlang
.js$ suffix-exact-match Items that end with .js
!.go$ inverse-suffix-exact-match Items that do not end with .go

White space acts as an AND operator, while a single pipe (|) character acts as an OR operator.

:::: tabs ::: tab List

const books = [
  {
    title: "Old Man's War",
    author: 'John Scalzi'
  },
  {
    title: 'The Lock Artist',
    author: 'Steve'
  },
  {
    title: 'Artist for Life',
    author: 'Michelangelo'
  }
]

::: ::: tab JS

const options = {
  includeScore: true,
  useExtendedSearch: true,
  keys: ['title']
}

const fuse = new Fuse(books, options)

// Search for items that include "Man" and "Old",
// OR end with "Artist"
fuse.search("'Man 'Old | Artist$")

:::

::: tab Output

[
  {
    "item": {
      "title": "Old Man's War",
      "author": "John Scalzi"
    },
    "refIndex": 0,
    "score": 2.220446049250313e-16
  },
  {
    "item": {
      "title": "The Lock Artist",
      "author": "Steve"
    },
    "refIndex": 1,
    "score": 2.220446049250313e-16
  }
]

::: ::::