Displaying Categorized/Filtered Results

is there a way to display categorized hits/results?

ex: I want to display the results categorized by status

Open
hits…

Pending
hits…

Closed
hits…

anyone? Is what i’m asking even possible? Or is there any way to do it?

Hi @jdonald, this is definitely possible! Have a look at the Distinct feature for grouping results.

You will need to:

  • enable distinct on your index settings.
  • set attributesForDistinct to the attribute you want to group by, here status
  • at query time, set distinct=3 if you want to get results from 3 different status values

You can control how many results you get for each group with nbHits and the maximum amount of results with hitsPerPage. for example:

  • hitsPerPage=3, nbHits=1 would bring a single result for each category
  • hitsPerPage=10, nbHits=4 would show up to 4 results from each category with a maximum of 10 results.

Let me know if this helps!

hi @pln,

thanks for replying, I will try this, but i’m not sure if this is what i need, using Distinct, can i have seperate paginations for each category?

I basically want to seperate the hits by category

Hi @jdonald,
I’m not sure how pagination would work in such a case: I would expect it to respect the same constraints across pages (so taking my first example of hitsPerPage=3, nbHits=1 I think each page would show three results, one of each category), but I would test and confirm that behavior.

If you want three independent lists of search results with each a different set of parameters (so each one can be on a different page, or with different filters), then you should probably use three separate indexes.

EDIT: My teammates from the is.js team pointed to this example, although it doesn’t use pagination you can start from this!