Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping order for query like "饾憥 AND 饾憦 AND 饾憪" #34

Open
laggingreflex opened this issue May 9, 2020 · 0 comments
Open

Comments

@laggingreflex
Copy link

It seems the query: "饾憥 AND 饾憦 AND 饾憪" is by default grouped as "饾憥 AND (饾憦 AND 饾憪)".

Would it be unreasonable to expect it be grouped instead as "(饾憥 AND 饾憦) AND 饾憪"?

I'm creating a filter based on this library, and I stumbled upon a particular query that makes me thing that the latter might be more natural.

E.g.: For this data:

const data = [
  { /* 0 */ name: 'C-3PO', species: 'Droid', height: 1.7526, misc: {} },
  { /* 1 */ name: 'R2-D2', species: 'Droid', height: 1.1, misc: {} },
  { /* 2 */ name: 'Anakin Skywalker', species: 'Human', height: 1.9 },
  { /* 3 */ name: 'Obi-Wan Kenobi', species: 'Human', height: 1.8, misc: {} },
  { /* 4 */ name: 'Han Solo', species: 'Human', height: 1.8, misc: {} },
  { /* 5 */ name: 'Princess Leia', species: 'Human', height: 1.5, misc: {} },
];

If I query:

an AND NOT wan AND NOT han

I expect the result to be

{ /* 2 */ name: 'Anakin Skywalker', ... }

right?

But that happens only when the query is specifically formatted as:

(an AND NOT wan) AND NOT han

To elaborate step-by-step:

Case 1: 'an AND NOT wan AND NOT han'

Query split as

{
left: 'an', 
operator: 'AND NOT', 
right: 'wan AND NOT han'
}
  1. Parse left side 'an' = 3 results: [Anakin, Obi-Wan, Han Solo]

  2. Parse right side: 'wan AND NOT han'

    Query split as:

    {
      left: 'wan', 
      operator: 'AND NOT', 
      right: 'han'
    }
    1. Parse left side 'wan' = 1 result: [Obi-Wan]

    2. Parse right side 'han' = 1 result: [Han Solo]

    3. Apply operator AND NOT

      [Obi-Wan] AND NOT [Han Solo] 
      

      = 1 results: [Obi-Wan]

  3. Apply operator AND NOT

    [Anakin, Obi-Wan, HanSolo] AND NOT [Obi-Wan]
    

    = 2 results: [Anakin, Han Solo]

End Result: [Anakin, Han Solo]

Case 2: '(an AND NOT wan) AND NOT (han)'

Query split as:

{
  left: 'an AND NOT wan', 
  operator: 'AND NOT', 
  right: 'han'
}
  1. Parse left side 'an AND NOT wan'

    Query split as:

    {
      left: 'an', 
      operator: 'AND NOT', 
      right: 'wan'
    }
    1. Parse left side 'an' => 3 results: [Anakin, Obi-Wan, Han Solo]

    2. Parse right side 'wan' => 1 results: [Obi-Wan]

    3. Apply operator AND NOT

      [Anakin, Obi-Wan, Han Solo] AND NOT [Obi-Wan] 
      

      = 2 results: [Anakin, Han Solo]

  2. Parse right side 'han' = 1 results: [Han Solo]

  3. Apply operator AND NOT

    [Anakin, Han Solo] AND NOT [Han Solo]
    

    = 1 results: [Anakin]

End Result: [Anakin]

So, as you can see only Case 2 gives the expected result.

Unless my expectations or algorithm is flawed in which case I'd appreciate the correction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant