0
votes

I am new to using Algolia. I'm trying to send user data to Aloglia from my Firestore so that I can search for them later in the app I am creating. I've successfully set everything up, all my functions work via the Firebase cloud function. So when I create a document for 'users' in Firestore it passes all of the fields to Algolia, updating and deleting this data is also manifested in Algolia.

However, for the sake of maintaining security, I do not want to send ALL of the user data to Algolia, but rather only a few fields. These include 'displayName' and 'username' (two of the fields in the 'users' collection).

So my question is how do I change my code to only send those two fields to Algolia? Please provide an answer for delete and update as well.

My Code:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');

const ALGOLIA_APP_ID = "MY KEY";
const ALGOLIA_ADMIN_KEY = "MY KEY";
const ALGOLIA_INDEX_NAME = "users";

var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);


admin.initializeApp(functions.config().firebase);

exports.createUser = functions.firestore
.document('users/{userID}')
.onCreate( async (snap, context) => {
    const newValue = snap.data();
    newValue.objectID = snap.id;

    var index = client.initIndex(ALGOLIA_INDEX_NAME);
    index.saveObject(newValue);
});

exports.updateUser = functions.firestore
    .document('users/{userID}')
    .onUpdate( async (snap, context) => {
        const afterUpdate = snap.after.data();
        afterUpdate.objectID = snap.after.id;
        
        var index = client.initIndex(ALGOLIA_INDEX_NAME);
        index.saveObject(afterUpdate);
    })

exports.deleteUser = functions.firestore
    .document('users/{userID}/')
    .onDelete( async (snap, context) => {
        const oldID = snap.id;

        var index = client.initIndex(ALGOLIA_INDEX_NAME);
        index.deleteObject(oldID);
    });
1

1 Answers

1
votes

Algolia has some good documentation on how to perform add, update, and delete operations on an index. Note that for adding and updating you will need to pass objectID.

Add only certain fields

Using some of the code from your example, here is how you could pass only certain fields of your Firestore object to Algolia (example from docs):

exports.createUser = functions.firestore
    .document("users/{userId}")
    .onCreate((snap, context) => {
        const newValue = snap.data();
        const user = {
            objectID: context.params.userId,
            displayName: newValue.displayName,
            username: newValue.username,
        };

        return index.saveObject(user);
    });

Update only certain fields

Also inspired by an example in the docs:

exports.updateUser = functions.firestore
    .document("users/{userId}")
    .onUpdate(async (snap, context) => {
        const afterUpdate = snap.after.data();
        const updateUser = {
            objectID: context.params.userId,
            displayName: afterUpdate.displayName,
            username: afterUpdate.username,
        };

        await index.partialUpdateObject(updateUser);
    });

Deleting

It is possible to delete by filters, though I've not done this, and the docs indicate that you should use the deleteObject method instead because it is more performant. You can do:

exports.deleteUser = functions.firestore
    .document("users/{userId}")
    .onDelete((snap) => {
        return index.deleteObject(snap.id);
    });