Hi, I'm trying to implement the index search for Fuse, but for some reason it's not working.
My error is
fuse_js__WEBPACK_IMPORTED_MODULE_27___default.a.createIndex is not a function
import Fuse from 'fuse.js'
const sampleRecipes = [
{
id: 1,
name: 'Plain Chicken',
servings: 3,
cookTime: '1:45',
instructions: "1. Put salt on chicken\n2. Put chicken in oven\n3. Eat chicken",
ingredients: [
{
id: 1,
name: 'Chicken',
amount: '2 Pounds'
},
{
id: 2,
name: 'Salt',
amount: '1 Tbs'
}
],
authors: [
{
id: 1,
name: "nun",
email: "[email protected]"
},
{
id: 2,
name: "matt",
email:'[email protected]'
}
]
},
{
id: 2,
name: 'Plain Pork',
servings: 5,
cookTime: '0:45',
instructions: "1. Put paprika on pork\n2. Put pork in oven\n3. Eat pork",
ingredients: [
{
id: 1,
name: 'Pork',
amount: '3 Pounds'
},
{
id: 2,
name: 'Paprika',
amount: '2 Tbs'
}
],
authors: [
{
id: 1,
name: "pun",
email: "[email protected]"
},
{
id: 2,
name: "mun",
email:'[email protected]'
}
]
}
]
const fuseListforIndex = [...sampleRecipes];
const index = Fuse.createIndex(
['name',],
fuseListforIndex
)
const options = {
keys: ['name']
}
const myFuse = new Fuse(fuseListforIndex, options, index)
const result = myFuse.search('Pork')
no search produces sample code in JS. And the documentation only has a typescript example.
I've updated the docs for createIndex
Hi Krisk, Really appreciate this! I was wondering if I had to put the data type in an array and pass that to the createIndex function, I was really confused! thanks for the JS version!
in case it helps anyone, if you want to pre-build the index this worked for me:
const options = { keys: ['title', 'author.firstName'] }
// Create the Fuse index
const myIndex = Fuse.createIndex(options.keys, books)
// Serialize and save the Fuse index
fs.writeFileSync('fuse-index.json', JSON.stringify(myIndex.toJSON()));
// require/fetch the index somehow
const fuseIndex = await require('/assets/fuse-index.json');
// initialize Fuse
const fuse = new Fuse(books, options, Fuse.parseIndex(searchIndex))
disclaimer: I'm not sure if this is officially supported or even the right way to do it!
This is right way to do it, and officially supported.
Most helpful comment
in case it helps anyone, if you want to pre-build the index this worked for me:
disclaimer: I'm not sure if this is officially supported or even the right way to do it!