Logical $and does not support multiple $or expressions
See original GitHub issueDescribe the bug
A clear and concise description of what the bug is.In an $and
logical expression, according to the docs, multiple expressions can be contained and they must all evaluate to true to satisfy the condition. However, if more than one $or
expression is contained in $and
, all apart from the first will be ignored.
Version
The Fuse.js version where this bug is happening.6.2.0
Is this a regression?
Don’t know
🔬Minimal Reproduction
Data:
[
{
"title": "Old Man's War",
"author": {
"firstName": "John",
"lastName": "Scalzi",
"age": "61",
}
}
]
Main:
const options = {
keys: [
"title",
"author.firstName",
"author.lastName",
"author.age",
]
};
const fuse = new Fuse(list, options);
return fuse.search({
$and: [
{ title: 'old' }, // Fuzzy "old war",
{
$or: [
{ 'author.firstName': "j" },
{ 'author.lastName': "Sa" },
]
},
{
// This should return no result, since nobody has age === 62
$or: [
{ age: "'62" },
// ...
]
},
]
})
Result:
[
{
"item": {
"title": "Old Man's War",
"author": {
"firstName": "John",
"lastName": "Scalzi",
"age": "61"
}
},
"refIndex": 0
}
]
Additional context
If you move the expression:
{
$or: [
{ age: "'62" },
// ...
]
}
before the first $or
, you’ll see it gets correctly picked up and no result is returned.
Related to #411.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Combining and Negating Conditions with AND, OR, and NOT
You can specify multiple conditions in a single WHERE clause to, say, ... AND, OR, and a third operator, NOT, are logical operators....
Read more >Logical AND (&&) - JavaScript - MDN Web Docs
The logical AND (&&) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands...
Read more >Using IF with AND, OR and NOT functions - Microsoft Support
Use the IF function along with AND, OR and NOT to perform multiple evaluations if conditions are True or False. The condition you...
Read more >Review: Logic and if Statements (article) | Khan Academy
This is a review of what we covered in this tutorial on logic and if statements. We often want to be able to...
Read more >Logical operators - The Modern JavaScript Tutorial
As we can see, the result is always true except for the case when both operands are false . If an operand is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’re right! I overlooked the
e-8
and got confused because the searched keyword did not appear in that result at all. Here is a repo that reproduces the issue https://github.com/belvederef/fuse-449.If you start the project and navigate to
http://localhost:5050/api/predicate?search=child
to search the wordchild
, you will see the results are all messed up.Preview of the results
However, if you change the
fuse.js
version in the package.json to6.2.0
, the results are correct.Preview of the results
Apologies if this is not the simplest setup. let me know if you have any questions.
@krisk thanks for looking into this. I am testing your new solution but it does work properly since the score is not calculated (it is
null
) and, therefore, there is no sorting. If you include the option flagincludeScore: true
in the example you posted above you will see this. I believe unit tests missed this particular case.