How to efficiently return all unique records for a facet value along with record counts

I’m using Algolia to power search in my app. I have an index called prod_COACHES in which I have some records with an object key called speciality1 .

The data structure for speciality1 looks like this:

I have enabled speciality1.itemName as an Algolia ‘facet’ so that I can filter on it. All good so far and working nicely. Now, in my Algolia dashboard I can see a nice bit of UI that shows me every unique facet (in this case my specialisations) along with the number of records for each facet:

image

As it happens, I want to show exactly this information on my own UI in my app but I’m not sure how to get this data from Algolia in the most efficient way. I’m using the client side AlgoliaSearch Javascript SDK. How do I run a search to retrieve every unique speciality1.itemName and the number of records for each unique speciality1.itemName so I can build my own UI just like the above?

I have gone through the docs and followed the examples but my question is really about finding the most efficient way to do this from someone who really knows Algolia well, rather than hack my own solution together. Thanks!

Hi @lifec, welcome to the forum!

I understand you’d like to build a UI like the one shown in the dashboard, showing all unique values for speciality1.itemName and their counts and that you’re using the JS API client. One quick question – any reason you’re not using one of our InstantSearch libraries? If so, you could use the refinementList widget/component to create this pretty quickly.

If you’d like to build it out from scratch, you could use the searchForFacetValues method with an empty query. E.g.:

index.searchForFacetValues({
  facetName: 'speciality1.itemName',
  facetQuery: ''
}) ...

This method returns values + counts. By default, maximum 10 results are returned . This can be adjusted via maxFacetHits. In order to use this method, make sure that speciality1.itemName is designated as a searchable attribute for faceting.

Hope this is what you’re looking for!

1 Like

Thanks for your help Maria!

That worked perfectly.

To answer your question on why I’m not just using an InstantSearch library; I’m enjoying building the UI manually and learning more about Algolia in the process.

Thanks so much for your help.