Approach to display contents based on user's preferences without running into query limits

Hi,

We have a news feed (of articles) that is based on Algolia search. We want to rank the feed based on following criteria

  • news stories from other people and tags we follow should come on top
  • news feed should not display static content, there should be some amount of shuffling in the news feed.

To solve this, I am generating a query which has tags and user id’s that the user follows to fetch relevant news feed. [News feed index has tags and user id’s in the index for each news feed record]. All the keywords in the query should be optional i.e OR between various query phrases rather than AND.

I am running into issues that the query length crosses the 512 byte query phrase limit, when there are a lot of tags and users the given user is following.

Is there a way to achieve the same results using a different approach rather than generating a really long query with tags and users Ids and using optionalWords search setting to achieve my goals?

I was chatting with one of my colleagues about this and he suggested a better approach would be to add the tags and users as optionalFilters within your search rather than chain them using ORs in the query string. This will bring records that match the users and tags to the top of the result set. You can even use filter scoring if you want to push certain tags or users higher in the results.

You can have up to 500 optional filters in a query, which should give you a lot more flexibility with longer lists of followed tags/users.

index.search('', {
    optionalFilters: "user:1234, user:5555, tag:breaking_news<score:100>", 
    hitsPerPage: 50,
}).then(({ hits }) => {
    console.log(hits.map(item => `- ${item.title} | ${item.tag} | ${item.objectID} | ${item.price_range}`).join('\n'));
});

More about optionalFilters:

More about filter scoring:

Thanks Chuck! Yes this helps with my use case. I just did a quick test using the dashboard and this seems to be working.
Thanks a lot for your prompt help!

1 Like