Multiple Filters / Boolean Search? filter (X AND Y) OR Z is not allowed, only (X OR Y) AND Z is allowed

Hi Community!

I have an interesting problem I need to solve regarding who can see particular records.

Overview
We have products that can be either private or public.
Products can also be restricted at the company level or the user level (users belong to companies), regardless if it is a private or a public product.
Private products have a list of companies or users that have access to that product.

My object structure looks like this:

{
    objectID: "1",
    name: "Product 1",
    public: true,
    allowed: []
    disallowed: ["company|1", "user|1"]       // company 1 and user 1 should not be able to see this product on any search
}

{
    objectID: "2",
    name: "Product 1",
    public: false,
    allowed: ["company|2", "user|2"]      // Only company 2 and user 2 should not be able to see this product on any search
    disallowed: []        
}

My initial solution was to use filters on search, and I came up with this logic:

(Search performed by user1, that belong to company 1)

{
  "filters": "(public:true AND NOT disallowed:company|1 AND NOT disallowed:user|1) OR (public:false AND (allowed:company|1 OR allowed:user|1))"
}

This should in theory work, but when I try to execute this it does not let me do it since Algolia for performance reasons does no support “(X AND Y) OR Z”:
filter (X AND Y) OR Z is not allowed, only (X OR Y) AND Z is allowed

Does anyone have an idea on how to overcome this?
I will be able to modify the index structure if needed.

Thank you so much for your help Community!

This is a limit I’ve also hit on a project: Searching for multiple matches in a single item at once

Unfortunately, we weren’t able to find a workaround, so we abandoned the idea of using Algolia there… :confused: