Using blog post tags as a facet

I came across Algolia while reading Netlify’s blog.

I have a few Jekyll based sites and want to implement Algolia search on all those sites. I could get the basic search working but not able to fully accomplish my requirements. Here’s a brief of my requirements and the challenge I’m facing.

I have blog posts and each post has multiple tags (or labels). I want to use facets for tags such that when user clicks on a tag name, it only matches the “tags” attribute of the blog post, not the title or the content or any other attribute of the post. Let’s take an example.

Title: Blog Post 1
Content: Sample content for blog post 1 containing keywords tag1, tag2, tag3, tag4, tag5.
Tags: tag1, tag2, tag3

Title: Blog Post 2
Content: Sample content for blog post 2 containing keywords tag1, tag2, tag3, tag4, tag5.
Tags: tag2, tag3, tag4

Title: Blog Post 3
Content: Sample content for blog post 3 containing keywords tag1, tag2, tag3, tag4, tag5.
Tags: tag2, tag5

I want the facets and respective counts to appear like this:

tag1 (1)
tag2 (3)
tag3 (2)
tag4 (1)
tag5 (1)

However, since the content of all 3 blog posts contain all 5 tags (tag1, tag2, tag3, tag4, tag5), the facet count is much higher that what I need it to be. As mentioned earlier, I want the facet count to only count the matches of the “tags” attribute, and not the “Content” or other attributes. Is this possible?

Another related question -

Can the tag name be made a hyperlink inside blog post content such that clicking on that link brings the users to the Search page (that has Algolia search enabled) and directly how the results matching that tag. Is this possible? I’m not sure how to accomplish this.

Thanks in advance.

1 Like


Thanks for contacting us!
The case you described and the results you are expecting are handle by the facets. When indexing you objects, you just need to use and array of strings for the tag attribute, like this:

  "title" "Blog Post 1",
  "content: "Sample content for blog post 1 containing keywords tag1, tag2, tag3, tag4, tag5."
  "tags": ["tag1", "tag2", "tag3"]

For the second question, yes it is possible. You can accomplish that for instance with the algoliasearch-helper library. It provides a convenient URL helper.

Here is a short snippet:

let client = algoliasearch("YOUR_APP_ID", "YOUR_SEARCH_API_KEY"); 
let helper = algoliasearchHelper(client, "YOUR_INDEX", {
  facets: ['login']

helper.addFacetRefinement('tags', 'tag1');
let queryString = algoliasearchHelper.url.getQueryStringFromState(helper.getState());

// output: q=&f[0]=tags&idx=YOUR_INDEX&p=0&fR[tags][0]=tag1

Let me know it that worked for you!

Thank you, Karim.

Maria from your team is also helping me via email. Interactions with her have helped me learn a lot about Algolia.

I’m using Jekyll extension and it do the push using that extension. Maria helped me resolve both the issues.

On the first issue, I’m using distinct, attributeForDistinct and facetingAfterDistinct parameters to achieve the result that I want.

On the second issue, I can compose the URL myself as I understand the query parameters now, without using the helper.

So both issues have been resolved. There are still some other open issues that I’m discussing with her.

Thanks again!

Im a newbie, so where do you find the actual files that you can add or modify files? I only see the main icons on the left and not much else to find files or create files…please help…thanks Alan