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.

[FEAT] Add ability to combine filters into OR arrangement

See original GitHub issue

Is your feature request related to a problem? Please describe. Would be awesome if it was possible to include a search all input which could technically be coded as several filters set to the same value. The problem is there is no way to tell the data provider how to combine these values (which ones are AND and which ones are OR). In some providers I’ve seen these filters just passed on with this being left up to them, for example strapi I think does an AND and if there are 2 filters with the same field it uses OR for those.

Describe the solution you’d like It would be great if it was possible to include filters in a more complex arrangement that allows for OR constructions being passed to providers while searching, for example:

filter = [
    {
      operator: "eq",
      field: "age",
      value: 20
    },
    {
      operator: "or",
      value: [
        {
          field: "title",
          operator: "eq",
          value: "Test",
        },
        {
          field: "description",
          operator: "eq",
          value: "Test"
        }
      ]
   }
]

Here the query would look like "age" == 20 AND ("title" == "Test" OR "description" == "Test")

This could be adopted gradually by data providers that support it (for example this would be possible in the Airtable Provider) and ignored by providers that don’t.

Additional context In the example on https://refine.dev/docs/guides-and-concepts/search/table-search/ this is solved with the q field but this must be available on the database.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
IgnusGcommented, Mar 1, 2022

Hey @IgnusG,

As you said, I’m trying to support the “or” operator, but I’m stuck while defining the type the CrudFilter. I explained what I want to do in more detail on here. I would be glad if you can help with this.

Hi @salihozdemir,

You can use discriminated unions for this. What you do is you define an extra OrFilter with something like:

type OtherFilter = {
  field: string;
  operator: Exclude<Operators, "or">;
  value: any;
}

type OrFilter = {
  // we omit the field property here
  operator: "or";
  value: Filter[];
}

And then you use unions to define the Filter as follows:

type Filter = OrFilter | OtherFilter;

Typescript can use type narrowing based on the operator after this. If the operator is “or” it knows we’re dealing with an OrFilter and it won’t offer the field property. If it’s anything else it knows it must be an OtherFilter with that property.

The important part on why this works here is that there is something that both types have (in our case the operator property) and this property has a different value in each union (either “or” or the other operators). You can read more about how this works here https://css-tricks.com/typescript-discriminated-unions/

Hope this helps! 👍

2reactions
salihozdemircommented, Mar 1, 2022

@IgnusG 🥇 You saved my day man, thank you so much for your detailed explanation 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

FEAT/UserGuide - FSL - FslWiki
Use Higher-level analysis for combining first-level analyses. ... filtering as it does not introduce autocorrelations into the data.
Read more >
Solved: Multiple filters on gallery with isblank
Hi All,. I am really struggling to insert some advanced filtering on my gallery. Current have the following: Filter( ProductionCollection,
Read more >
Streamline Multiple Gmail Filters into one Filter - BetterCloud
Gmail filters are some of the best ways to use the power of Google to automate tasks inside of Gmail. If you find...
Read more >
Best Practices for Segments - the Feathr Help Desk
This article teaches segmentation methods and best practices by discussing how to use Feathr's filtering capabilities to build some useful ...
Read more >
Dashboard Data Expression to Combine Multiple Feature Sets ...
Solved: Hi everyone! I have another question regarding one of my data expressions in ArcGIS Dashboards. I have four layers published to a ......
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