Reputation: 722
I am working on building out some filtering logic using the “AND” and “OR” operators and I am wondering if what I want is even possible…Each of our records have a set of string “tags” on them and which records are shown is configurable by our users. The filtering logic that we’re trying to implement is that the record will not be shown if the users configuration has blocked ALL the tags on a record.
For example, record 1 has the tags [“one”, “two”, “three”], and the the user has blocked tags “one” and “two”…In this scenario record 1 would still be shown in the results because tag “three” isn’t blocked. If the user were then to add tag “three” to blocked, only then will it not show up in the results list because all of record 1’s tags have been blocked.
I’ve tried some different groupings with () brackets and the like but everything that I’ve tried always results in record 1 being blocked if any of the tags is configured to be blocked.
This is an example of what I would imagine should work but has still resulted in blocking the record I am trying to fetch:
filters: [(NOT tag: "one" AND NOT tag: "two")]
record 1: { ..., tag: ["one", "two", "three"] } <-- this record gets filtered
Is there a way to only filter record 1 out when ALL tags match the filters with Algolia filters or will I need to do something else to accomplish this behavior?
Upvotes: 1
Views: 394
Reputation: 1
It is expected that the filters you have shared would filter out that record since it is a match.
I would say that there is a problem on the data structure that you have set to achieve your purpose.
If your users wants to filter out records containing tag: one
, it does makes sense to filter out every record containing tag: one
, even though these records also contain other tags.
If your users wanted to only filter out products that contains every tag, then you need a different filter.
Your records and filter should look like this:
record 1: { ..., tag: ["one", "two", "three", "one-two", "one-two-three", ...] },
record 2: { ..., tag: ["one", "two", "three", "one-two"]}
filters: NOT tag: "one-two-three"
In this example, you would filter out record 1, and retrieve record 2.
With this data structure you would allow your users to filter out based on different combinations or individual tags.
Upvotes: 0