Frontend filtering/ranking

I’m trying to implement contact search bar, for now, it worked like a charm, until i wanted users, who’s already friends with the current logged in user to show up first, and then show the rest, something like social media sites.
Any idea how i can implement this functionality using algoliasearch?

Hey @ahmed.ben.neji,

I’m working on implementing search with friends/following now.

Found this thread pretty helpful thus far.

But to summarize ( at least what I got out of reading that )

  • If number of friends will never be very large -> Add a facet for each friend when executing the search
  • If number of friends becomes large -> Add an array of “friends” to each user record that contains user ids. Facet on that array for current user ids. I think using this method you should be able to store enough user ids for MOST users. With that being said, if you have a user who has more friends than will fit on a single record, you will need to break up these records and then use distinct.

Would love if someone who’s done this before could chime in here and either correct me or confirm what I’ve said.

Hello,

If I understand it correctly your use-case is a little simpler than the one linked in the above post. What I understand is that you want to search in a list of users but would like the friends of the current logged in user to be ranked higher.

To do that I’ll make sure to have a key on each record (let’s name it friends_with) that contains an array of all the user_id this user is friend with. You’ll need to define this key as one of the attributesForFaceting to be able to filter on it.

Now in your front-end you’ll be able to get the user_id of the currently logged in user and use it to limit the results. You shouldn’t use a regular filter to do that, as a regular filter on this friends_with: {current_user_id} will only return friends, and you want to return friends AND other results.

To do that, you’ll have to use the optionalFilters option. The difference with a regular filter is that instead of only returning elements that match the filter, it will return everything, but will put the elements matching at the top of the results. That way you’ll be sure to always have friends on top, and other people below.

Hope that helps :slight_smile:

1 Like