How to hide search results conditionnaly?

Hi,
I’d like to hide search results conditionnaly when an attribute has a specific value. My index contains posts and each has a status attribute which can take one of these values : [‘draft’, ‘pending’,‘gone’,‘published’]. Data sources is a Firestore database and I use the Algolia Extention. All data are systematically indexed, but I’d like to index or show only data with status [‘published’]. Transform functions don’t seem to work.
And in Algolia documentation I didn’t find any solution for my use case (like to hide search results conditionnaly).
Thank you in advance for your help :wink:

Hello!

You have a few options I can think of off the top of my head:

  1. Transform functions
import * as functions from "firebase-functions";

// Set this up as a function in Firebase
export const transformPostForSearch = functions.https.onCall((payload) => {
    // Check if the Post is published, otherwise stop the sync to Algolia here
    if (payload.status !== 'published') {
        throw new functions.https.HttpsError("failed-precondition", "Post is not yet published.");
    }
    // Otherwise, pass it along to Algolia
    return payload;
});
  1. Use the filter option
$results = $index->search('some-query', [
  'filters' => 'status:published'
]);
  1. Mutate the results in transformItems (not recommended since Users could still see these results if they poked around in the network tab)
search.addWidget(
  instantsearch.widgets.hits({
    // ...
    container: document.querySelector("#posts"),
    transformItems: (items) => items.filter(item => item.status === 'published')
  })
);

Let me know how this works for you, happy to help further. Thanks!

2 Likes

Hi Michael.king,

Thank you for your response. I tried to use Transform functions, but it seems that the Algolia Extension doesn’t call the transform function, although it is registrered in the extension parameters. I added a log into the function to check and I have nothing in logs when a doc is indexed in Algolia. I found this article https://dev.to/maxmonteil/firebase-algolia-how-to-set-up-transform-functions-35. The problem is the official documentation is weak on the Transform function and there is no example on the web.

I also tried to apply a rule in algolia with a filter but it seems that it is not possible.

And if I using the filter option, then the filter is only applied on the client side :frowning:

I’m going to do some more tests based on your suggestions.

Thank you :wink:

@itservices1 I recorded a video on how I got the Transform Functions to work, take a look if you would like:

1 Like

Hi Michael,

Thank you so much for making a video about the Transform function :clap:

I followed all the steps, and in my project I noticed that the Algolia Extension wasn’t installed on the same Cloud region as the Transform function (europe-west1 <-> us-central1). This is probably why my function was never called ? Now I installed the Algolia Extension on the same region as the function and everything works fine !

Thank you for your help !

1 Like