How to have a filter that allows for private and public content?

Hello! I’m a bit stuck with my particular use case. Essentially users can have content that is private to them or shared with everyone. The index contains a user_id field and a shared field. I generate a secured API key which should have filters that enforce this rule for each user. The obvious solution is to do shared:true OR (shared:false AND user:user_id) however according to the docs (https://www.algolia.com/doc/api-reference/api-parameters/filters/) you can’t have disjunctions (OR) of conjunctions (AND), i.e. this solution doesn’t work.

Has anyone run into a similar use case and how did you deal with it? I’m considering of changing the index fields so instead of a shared field I have a can_access field which can either be a list of user ids, or all to signify that it’s shared with everyone. This way my filter could just be can_access:all OR can_access:user_id (which will match if the user_id is in the array of ids). Does this seem sane?

The other thing I could do just keep the shared content in Algolia and load the private content from my own DB when the app starts (since we know it won’t change without this user changing it). This seems fairly reasonable and the performance would be better since I could show their content immediately (no loading time).

Any and all opinions are welcome, thanks in advance :slight_smile:

Hi there,

That’s a pretty interesting question!

I am not sure why you would need the shared:false in your filter. You can invert the way to think about this problem and say: “I want all the data for user user_id OR publicly shared data”. With this formulation, I believe that shared:true OR user:user_id would work as expected.

That being said, access restriction that is not public/private is tricky and indeed, most of the time you will end up with an array of access permissions. Be careful though as these lists can grow quite large depending on the use case. There are some additional tricks that you can use once you reach this scaling issue like using groups and group_ids, but it becomes a lot more involved.

Let me know if that answered your question.
Best,

Thanks for that, makes sense! I can definitely see me eventually needing to use groups but for now I’ll try keep it simple :slight_smile: