Get all unique tags with its total counts

How can I get all the tags attribute and group by distinct as circled in the provided image? Each record may have tags and the tags may duplicates. I’ve tried to get this data by using the JavaScript API as seen below and it didn’t work. I expect it to show all the unique tags as circled but instead, it returns every record. Any idea on how to achieve the desired outcomes?

import algoliasearch from "algoliasearch";

const algoliaClient = algoliasearch(

const algoliaIndex = algoliaClient.initIndex("main-index");

  attributeForDistinct: "tags",
  attributesToRetrieve: ["tags"],
  distinct: 1
});{ hits }) => {

Hi @afiq.rosli,

If I understand correctly, you are looking to retrieve all the possible unique values for the ‘tag’ attribute, is that correct?

If this is it, the method you are looking for is searchForFacetValues.

You’ll need first to declare the tags attribute in the attributesForFaceting index setting with the searchable() modifier.

Then you can make a search this way:

index.searchForFacetValues('tags', '')

You can also use the maxFacetHits optional parameter to increase the number of results in case the original request does not return all the results.


1 Like