Stefan Schuh
Stefan Schuh

Reputation: 43

How to Filter Documents Based on Multiple Conditions Using Algolia (Visibility and User-Specific Rules)

I am building an application where I need to display documents based on multiple conditions related to visibility and user relationships. Here’s the logic I want to implement:

  1. Show all documents where visibility is "public".
  2. Show all documents where createdBy is the current user (no matter the visibility).
  3. Show documents where: visibility is "followers" createdBy is a user that the current user follows.

I initially tried to implement this directly using Firestore, but ran into limitations with full-text search and Firestore’s inability to evaluate follower relationships dynamically within security rules.

What I've Tried with Algolia:

I switched to using Algolia to handle this, and I set up the filters as follows:

filters: 'visibility:"public" OR createdBy:"currentUserId" OR (visibility:"followers" AND (createdBy:"followingUserId"))'

However, this results in the error:

ApiError: filters: filter (X AND Y) OR Z is not allowed, only (X OR Y) AND Z is allowed

My Question:

Is there a way to structure these filters correctly in Algolia to achieve the desired behavior given the limitation

(X AND Y) OR Z is not allowed, only (X OR Y) AND Z is allowed

?

If this approach is not suitable, what is the recommended method for handling this type of visibility filtering in a scalable and efficient manner using Algolia or any other approach compatible with Firestore?

Any guidance or best practices would be greatly appreciated. Thank you!

Upvotes: 1

Views: 52

Answers (0)

Related Questions