Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use createIndex in Javascript? #419

Closed
wongmrdev opened this issue May 19, 2020 · 6 comments
Closed

How to use createIndex in Javascript? #419

wongmrdev opened this issue May 19, 2020 · 6 comments
Labels

Comments

@wongmrdev
Copy link

wongmrdev commented May 19, 2020

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: "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
Copy link
Author

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

@krisk
Copy link
Owner

krisk commented May 30, 2020

I've updated the docs for createIndex

@wongmrdev
Copy link
Author

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
Copy link

roperzh commented Jun 3, 2020

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
Copy link
Owner

krisk commented Jun 13, 2020

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

@gauravverma029
Copy link

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants