krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
18.32k stars 772 forks source link

How to use createIndex in Javascript? #419

Closed wongmrdev closed 4 years ago

wongmrdev commented 4 years ago

Hi, I'm trying to implement the index search for Fuse, but for some reason it's not working. My error is

fuse_jsWEBPACK_IMPORTED_MODULE27default.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: "nunny@gmail.com"
      },
      { 
        id: 2,
        name: "matt",
        email:'mwong@gmail.com'      
      }
    ]
  },
  {
    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: "hat@gmail.com"
      },
      { 
        id: 2,
        name: "mun",
        email:'geo@gmail.com'      
      }
      ]
  }
]

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')
wongmrdev commented 4 years ago

no search produces sample code in JS. And the documentation only has a typescript example.

krisk commented 4 years ago

I've updated the docs for createIndex

wongmrdev commented 4 years ago

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!

roperzh commented 4 years ago

in case it helps anyone, if you want to pre-build the index this worked for me:

  1. In your build step
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()));
  1. In your app:
// 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!

krisk commented 4 years ago

This is right way to do it, and officially supported.

gauravverma029 commented 1 year ago

I am little confuse why we need again books as first parameter.becuase we already created index file using books varaible.if i am using only searchIndex with pass books this is not working // require/fetch the index somehow const fuseIndex = await require('/assets/fuse-index.json'); // initialize Fuse const fuse = new Fuse(books, options, Fuse.parseIndex(searchIndex))