question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use createIndex in Javascript?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
roperzhcommented, Jun 13, 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!

1reaction
kriskcommented, Jun 13, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

IDBObjectStore.createIndex() - Web APIs | MDN
The createIndex() method of the IDBObjectStore interface creates and returns a new IDBIndex object in the connected database.
Read more >
Indexes — Node.js - MongoDB
The following example uses the createIndex() method to create a compound index on the type and genre fields in the movies collection in...
Read more >
mongodb.Collection.createIndex JavaScript and Node.js code ...
How to use. createIndex. function. in. Collection. Best JavaScript code snippets using mongodb.Collection.
Read more >
create-index - npm
create-index program will look into the target directory. If there is no ./index.js , it will create a new file, e.g. ... Created...
Read more >
node.js - MongoDB ensurIndex and createIndex using nodeJS?
If you want to add an index to some field, just add index: true to its definition. So you can do the following:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found