Search code examples
javascriptvue.jsvuejs2algolia

Algolia - Get all unique tags with its total counts


enter image description here

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(
  process.env.VUE_APP_ALGOLIA_APP_ID,
  process.env.VUE_APP_ALGOLIA_ADMIN_KEY
);

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

algoliaIndex.setSettings({
  attributeForDistinct: "tags",
  attributesToRetrieve: ["tags"],
  distinct: 1
});

algoliaIndex.search().then(({ hits }) => {
  console.log(hits);
});

Solution

  • Thanks to Marie Gillier from the Algolia team in answering my question. Below is the solution. You can find more explanation from the link given (Algolia's Forum)

    algoliaIndex.searchForFacetValues("tags", "").then(({ facetHits }) => {
      console.log(facetHits);
    });