krisk / Fuse

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

How to find out which key "caused the match"? #461

Closed katerlouis closed 4 years ago

katerlouis commented 4 years ago
const people = [
  { firstName: "John", lastName: "Doe", job: "Example-Hero" },
  { firstName: "Peter", lastName: "Dinklage", job: "Actor", },
  { firstName: "Pooh", lastName: "The Bear!": "Well, A Bear! S#!T" },
];
const fuse = new Fuse(people, {
  keys: ["firstName", "lastName"],
  threshold: 0,
});

const result = fuse.search("Pet");

When the firstName caused the match, I want to display Peter Dinklage in my auto-completion, but if the lastName caused the match, I want to show Dinklage, Peter.

It would be great if each item in result could tell me what caused the match, just like refIndex does.

Or do you have any other idea how to get that information?

krisk commented 4 years ago

Set the option includeMatches to true.