krisk / Fuse

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

Fuse.createIndex() differs from Fuse() by not accepting keys in object notation (for weighted search) #446

Closed robertsass closed 4 years ago

robertsass commented 4 years ago

Describe the bug

When trying to bring in Fuse.createIndex() it gives me this error:

TypeError: path.indexOf is not a function. (In 'path.indexOf('.')', 'path.indexOf' is undefined)

Version

6.0.4

Is this a regression?

Don't know.

🔬Minimal Reproduction

function buildIndex( items, options ) {
  const defaults = {
    isCaseSensitive: false,
    shouldSort: true,
    minMatchCharLength: 2
  };
  const mergedOptions = {...defaults , ...options};
  return Fuse.createIndex( mergedOptions.keys, items );
}

const datasets = [
  {title: "Foo", description: "Superfooicious"},
  {title: "Bar", description: "Superbaricious"}
];

const options = {
  keys: [
    {name: 'title', weight: 2},
    {name: 'description', weight: 1}
  ]
};

const searchIndex = buildIndex( datasets, options );

Additional context

Nothing to say. But yeah I love Fuse.js!!!

krisk commented 4 years ago

That's fair a flag. I designed this on purpose, since generating the index is completely different, and does not require weights (since weighting is applied at query time).

Nonetheless, it feels like I could just support this functionality, so people don't have to deal with different arg formats.

I will do this for the next release. Thanks!

krisk commented 4 years ago

Now in latest fuse.js@6.2.0

robertsass commented 4 years ago

since weighting is applied at query time

So passing options (including keys) again as the second parameter to Fuse() does not cause it to rebuild it's index but really uses the one I pass as third parameter?