krisk / Fuse

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

Logical Operators with Extended Search Returning Unexpected Results #548

Closed nminchow closed 3 years ago

nminchow commented 3 years ago

Describe the bug

Logical operators with extended search expressions are not returning expected results.

Version

6.4.6

Is this a regression?

This is the first version I have used

🔬Minimal Reproduction

const Fuse = require('fuse.js');

const data = [
  {
    type: 'foo bar',
    name: 'hit',
    stub: 'hit',
  },
  {
    type: 'qux quz',
    name: 'miss',
    stub: 'miss',
  },
];

const db = new Fuse(data, {
  keys: ['name', 'stub', 'type'],
  includeScore: true,
});

const notQux = db.search({
  $and: [
    {
      type: '!^qux',
    },
    {
      $or: [
        { name: 'hit' },
        { stub: 'hit' },
      ],
    },
  ],
})[0];

console.log(notQux); // undefined

const isBar = db.search({
  $and: [
    {
      type: '^bar',
    },
    {
      $or: [
        { name: 'hit' },
        { stub: 'hit' },
      ],
    },
  ],
})[0];

console.log(isBar);
// {
//   item: { type: 'foo bar', name: 'hit', stub: 'hit' },
//   refIndex: 0,
//   score: 0.0021180204984211294
// }

Additional context

In the above example, I would expect the opposite behavior. If my understanding is correct, the first query should be returning the foo bar object, and the second query should be undefined.

krisk commented 3 years ago

Seems to work just fine:

Screen Shot 2021-05-08 at 9 32 16 AM
nminchow commented 3 years ago

Ah, I missed that I needed to supply the useExtendedSearch param to the initializer! Thanks for the working example.