Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
});
like image 539
Afiq Rosli Avatar asked Dec 07 '25 08:12

Afiq Rosli


1 Answers

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);
});
like image 195
Afiq Rosli Avatar answered Dec 08 '25 20:12

Afiq Rosli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!