mirror of
https://github.com/krisk/Fuse.git
synced 2026-01-18 16:16:25 +00:00
65 lines
1.3 KiB
Markdown
65 lines
1.3 KiB
Markdown
---
|
|
tags:
|
|
- indexing
|
|
---
|
|
|
|
# Indexing
|
|
|
|
### `Fuse.createIndex`
|
|
|
|
Pre-generate the index from the list, and pass it directly into the Fuse instance. If the list is (considerably) large, it speeds up instantiation.
|
|
|
|
**Example**
|
|
|
|
```js
|
|
const books = [
|
|
{
|
|
title: "Old Man's War",
|
|
author: {
|
|
firstName: 'John',
|
|
lastName: 'Scalzi'
|
|
}
|
|
},
|
|
{
|
|
title: 'The Lock Artist',
|
|
author: {
|
|
firstName: 'Steve',
|
|
lastName: 'Hamilton'
|
|
}
|
|
}
|
|
/*...*/
|
|
]
|
|
|
|
const options = { keys: ['title', 'author.firstName'] }
|
|
|
|
// Create the Fuse index
|
|
const myIndex = Fuse.createIndex(options.keys, books)
|
|
// initialize Fuse with the index
|
|
const fuse = new Fuse(books, options, myIndex)
|
|
```
|
|
|
|
:::tip
|
|
Fuse will automatically index the table if one isn't provided during instantiation.
|
|
:::
|
|
|
|
### `Fuse.parseIndex`
|
|
|
|
Parses a serialized Fuse index.
|
|
|
|
**Example**
|
|
|
|
```js
|
|
// (1) In the build step
|
|
// Create the Fuse index
|
|
const myIndex = Fuse.createIndex(['title', 'author.firstName'], books)
|
|
// Serialize and save it
|
|
fs.writeFile('fuse-index.json', JSON.stringify(myIndex.toJSON()))
|
|
|
|
// (2) When app starts
|
|
// Load and deserialize index
|
|
const fuseIndex = await require('fuse-index.json')
|
|
const myIndex = Fuse.parseIndex(fuseIndex)
|
|
// initialize Fuse with the index
|
|
const fuse = new Fuse(books, options, myIndex)
|
|
```
|