krisk / Fuse

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

Create Index With Weighted Keys #437

Closed sirius16 closed 4 years ago

sirius16 commented 4 years ago

I want to pre-index an object because I'm going to use it multiple times but when I go to do see I get an error.

After a little digging, it seems that you can only index with string keys? It says i.indexOf is not a function and the value of i is the object with the key name and weight

Would very much appreciate help with this, thank you

(P.S. I'm very appreciative of the logical query feature added in v6.0.0. Turns out that if you want to compare two arrays of objects, turning one of them into a (very long) logical query is the move)

krisk commented 4 years ago

Don’t index with keys weights. Weights are only used at search time, so they would be useless for indexing purposes.

In other words, pre-generate the index by passing in just the string array of keys, and apply key weights when setting the options during fuse instantiation only.

Example:

// When creating the index, ‘keys’ must be a string array.
const myIndex = Fuse.createIndex([“title”, “author”], list)

// When creating the Fuse instance with the index, you can assign weights to the keys
const fuse = new Fuse(list, {
  keys: [{ name: “title”, weight: 0.5}, { name: “author”, weight: 0.4 }]
}, myIndex)