How to update Algolia data to match changes in Firebase Firestore with Cloud Functions (Part 3)

How to update Algolia data to match changes in Firebase Firestore with Cloud Functions (Part 3)

Before I proceed with this piece, there are two other articles you should go through to give you an idea of what is going on. They are linked below.

  1. Full Text Search with Firebase on Android using Algolia(Part 1) — Link
  2. How to import data from Firebase Firestore into Algolia using Cloud Functions (Part 2) — Link
  3. How to update Algolia data to match changes in Firebase Firestore with Cloud Functions (Part 3) — You are here

Listening to changes in Firestore DB using cloud functions

So you have added your initial data to Algolia and your users are now experiencing a mind-blowing search experience. However, the data to be indexed is constantly changing. So what do you do in that situation as a developer? Do you go through step two and import the data once again? I do not need to tell you of the obvious problem with this approach.

That is why you can write functions to listen to changes in your Firestore DB. These changes can be either a delete, update, or add operation. At this point, you may want to know that step 2 is only relevant to you if you have an existing project with data. If you are starting a new project, you can just dive into this stage directly.

Update Algolia index once new data becomes available

So in your index.js file that you have been working on, add the following function. This is assuming your collection is called “posts” and has an id.

exports.addPostToIndex = functions.firestore.document('posts/{postId}')
   .onCreate(snapshot => {
        const data = snapshot.data();
        data.objectID = snapshot.id;
        return postIndex.saveObject(data);
    });

Update Algolia index once changes are made to an existing document

Assuming you have a document already in your collection, you can also update your Algolia index to match the changes. The code is as follows:

exports.updatePostInIndex = functions.firestore.document('posts/{postId}')
    .onUpdate((change) => {
        const newData = change.after.data();
        newData.objectID = change.after.id;
        return postIndex.saveObject(newData);
    });

Updating Algolia Index once a document is deleted

Say a user deletes a post. You do not want that post to be searchable and so you should go ahead and update your Algolia index as well to match the changes. You can do so simply as follows:

exports.deletePostFromIndex = functions.firestore.document('posts/{postId}')
   .onDelete(snapshot =>
       postIndex.deleteObject(snapshot.id)
   );

That’s all

As you have seen, this step is relatively easier than the others. For a new project, you are better off indexing from the start and making your life easier. Once you finish this step, you are ready to implement search functionality in your android app. The Algolia team has a great sample (in Kotlin of course) and it uses Architecture such as Paging, ViewModels, and LiveData to make your work easier.

Until next time! PS: Let us connect on GitHub , Twitter , and LinkedIn .