Top 5 results for each product "type"

I’m looking to build a query to get the top 5 results for each category for a search query.

The category is set as an attribute on the records. Is this possible in a single query?

Example: Top 5 hats, top 5 coats, top 5 shirts

Hi @adam6, You will need three queries to get the top 5 of each category. You could use the multipleQueries method but it would still be counted as 3 queries.

So I would need to know which 3 categories I would like to query, correct? I was hoping to get something that would automatically get the top 5 from the top 3 categories without knowing what those categories may be.

Hi @adam,

I’m not sure I fully understood your request. Let me recap to be sure we are on the same page: You currently have an Algolia implementation. You would like to have information about the top results of a given facet for a specific search term. Is it correct? If so, the Analytics API is the answer to this question. I can go in detail about the implementation, but I would like to be sure I’ve understood your request.

If it’s not the case, please provide as much context as possible to help us better understand!

Thanks for the reply.

I have a store with many products and each product has a type. For example, if a user searches “red”, I would like the top 5 items for every type with results to be shown. Similar to Amazon’s Best Sellers page which shows the top 3 products from each category.

Results page would look like this:

Top 5 “red” shirts
Top 5 “red” pants
Top 5 “red” shoes

But the product types are not known at the time of the search.

Let me know if I can provide more context. Thanks!

Hi Adam,

I think this needs to be decompose into smaller steps.

The first step would be to get the 3 categories that you consider “top categories”. With Algolia, those categories would be “facets” that can only be sorted by count (number of matching records for a facet) or alphabetically. If you have a different sorting algorithm for the facets, you’d need to list them yourself with your custom ranking logic.

Once you have listed the categories by order of popularity, as Maria suggested you will need to do a query for each category. To get the top 5 items per category, you’ll need some kind of score for each object and use our ranking algorithm to make sure they are shown first. So you need to write a logic that suits your business logic to get that score on each of your items. As Samuel pointed out, our analytics API can give you useful metrics to determine that score, such as popular results.

To summarise, what I suggest you do is decide how you want to compute the popularity of your categories/facets and items. Once you have that logic, our custom ranking formula will help you get the 5 most popular items that matches your query, in each of your most 3 popular categories.

I hope that makes sense. Let us know how it goes and if you have more questions!