question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logical $and does not support multiple $or expressions

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
belvederefcommented, Jun 25, 2020

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 word child, you will see the results are all messed up.

Preview of the results
child
http://dbpedia.org/ontology/child

displacement (μ³)
http://dbpedia.org/ontology/displacement

acceleration (s)
http://dbpedia.org/ontology/acceleration

top speed (kmh)
http://dbpedia.org/ontology/topSpeed

id
http://www.w3.org/ns/activitystreams#id

weight (g)
http://dbpedia.org/ontology/weight

width (μ)
http://dbpedia.org/ontology/width

organization
http://www.w3.org/ns/org#organization
Indica l'Organization in cui l'Agent è un membro.

member
http://www.w3.org/ns/org#member
Indica la Person (o un altro Agent) coinvolto in una relazione di Membership. È l'inverso di `org:hasMembership`.

transitive sub-organization
http://www.w3.org/ns/org#transitiveSubOrganizationOf
La version transitive de la propriété subOrganizationOf, renvoie une représentation de toutes les organisations qui contiennent celle-ci. Notez que ceci est une super-propriété de la relation transitive donc elle pourrait contenir des assertions additionnelles mais cet usage n'est pas recommandé.

equal (0..*)
http://purl.org/goodrelations/v1#equal
This ordering relation for qualitative values indicates that the subject is equal to the object.

architect
http://dbpedia.org/ontology/architect

However, if you change the fuse.js version in the package.json to 6.2.0, the results are correct.

Preview of the results
child
http://dbpedia.org/ontology/child

childMaxAge
http://schema.org/childMaxAge
Maximal age of the child.

childMinAge
http://schema.org/childMinAge
Minimal age of the child.

children
http://schema.org/children
A child of the person.

architect
http://dbpedia.org/ontology/architect

numChildren
http://schema.org/numChildren
The number of children staying in the unit.

child organisation
http://dbpedia.org/ontology/childOrganisation

children features
http://www.geonames.org/ontology#childrenFeatures
Links to an RDF document containing the descriptions of children features

left child
http://dbpedia.org/ontology/leftChild

right child
http://dbpedia.org/ontology/rightChild

parent-child property
http://purl.org/linked-data/cube#parentChildProperty
Specifies a property which relates a parent concept in the hierarchy to a child concept.

Apologies if this is not the simplest setup. let me know if you have any questions.

1reaction
belvederefcommented, Jun 23, 2020

@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 flag includeScore: true in the example you posted above you will see this. I believe unit tests missed this particular case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found