Dynamically rank hits based on current_user attributes

Hello there!

I would like to apply a custom ranking based on the current_user’s attributes.

As ranking is defined on the rails side, before indexing records, I am unsure if it is possible to apply ranking dynamically on the client side.

For a concrete example:
I am rendering a list of services available across Europe. My current_user is based in Germany. I would like to promote/rank top all services in Germany.

The end goal would be to promote “best matching” services, without filtering out alternative.

The difficulty is that the ranking depends on the user’s attributes and not on a general rule. All users should have their individual ranking and ranking should take multiple parameters like country but also business_size etc.

Any suggestions on how to apply ranking on the client side?
customRankings or optionalFilters might be the way to go but I am really uncertain.

Thank you very much for your assistance.
PS: Thank you for the great product you’re building!!!

Hi @julien1,

Thanks for contacting Algolia and we are glad you are enjoying the product! We are happy to provide general guidance since it sounds like you are at the initial stages thinking about data structure and your implementation.

I am unsure if it is possible to apply ranking dynamically on the client side.

Indeed, you are corrrect - no, you cannot apply ranking (or custom ranking) client side/dynamically. This is why you must consider other strategies.

For your “best matching” services, here are a few big picture approaches to consider:

  • Geo Search - If your current_user is based in Germany, include a _geoloc attribute in each of your services records. Then using aroundLatLngViaIP you can leverage the user’s IP address to find the Germany-based services
  • "Other Rankings" - You mention some other metrics you would like to use to limit results (below):

All users should have their individual ranking and ranking should take multiple parameters like country but also business_size etc.

Consider this, if possible, based on the current_user data from Rails:

  • “country” - Include a country attribute in your services, set as an attributesForFaceting, then filter on that country
  • “business_size” - Similarly have some analogous business_size attribute in your services, set as an attributesForFaceting, then use boolean filters to set a range such as “only return services with business_size > 500”

Any suggestions on how to apply ranking on the client side?
customRankings or optionalFilters might be the way to go but I am really uncertain.

Indeed, the client side application of this can be done with filters (which results in only the matching results) or optionalFilters (which would be more of “promoting” matching results, while still including all others thereafter).

Overall, you should make sure your current_user has data that can then be used to trigger certain filtering via Algolia attributes. How does that help you?

Hi @ajay.david,

Thank you for your prompt response :slight_smile:

As you suggested, the way to solve this would be to fetch the current user’s data (logged in users) and trigger the optionalFilters. At least it works well on the dashboard!

Thank you very much for the support!

1 Like