Export all records from Firestore to indices with Google Cloud Function

I am trying to index my existing firestore db with a google cloud function. I want to call the cloud function from a url, rather than by an event trigger like onCreate().

I have tried using the code on tut under the heading ’ Import Firebase data to Algolia’, which I interpret like this:

exports.sendCollectionToAlgolia = functions.https.onRequest(async (req, res) => {

    // This array will contain all records to be indexed in Algolia.
    // A record does not need to necessarily contain all properties of the Firestore document,
    // only the relevant ones. 
    const algoliaRecords = [];

    // Retrieve all documents from the COLLECTION collection.
    const querySnapshot = await admin.firestore().collection('Sessions').get();

    querySnapshot.docs.forEach(doc => {
        const document = doc.data();
        // Essentially, you want your records to contain any information that facilitates search, 
        // display, filtering, or relevance. Otherwise, you can leave it out.
        const record = {
            objectID: doc.id,
            sessionDate: document.sessionDate,
            customer1FirstName: document.customer1FirstName,
            customer1LastName: document.customer1LastName

    // After all records are created, we save them to 
    index.saveObjects(algoliaRecords, (_error, content) => {
        res.status(200).send("COLLECTION was indexed to Algolia successfully.");

but I get 408 request timeout. It does not seem to recognise database.ref(’/sessions’) syntax?

I am on the Firebase Blaze plan.

Hello there,

Not sure what you mean by:

It does not seem to recognise database.ref(’/sessions’) syntax?

From my perspective, it looks like this part of the code

res.status(200).send("COLLECTION was indexed to Algolia successfully.");

was never reached (hence the 408 request timeout, the request is never completed).

Could you maybe try to debug this part to understand why it didn’t reached it?

Hope that helps,

Hi I debugged and found it wasn’t producing the list to send to index.saveObjects() - now working with this code:

// Get all sessions from Firebase
exports.sendCollectionToAlgolia = functions.runWith(runtimeOpts).https.onRequest(async (req, res) => {

admin.firestore().collection("sessions").get().then((docs) => {

    let _sessions = [];

    docs.forEach((doc) => {

        let session = doc.data();

        const childKey = doc.id;
        if (
            session.customerInfo.customers.customer1.customerFirstName && 
        ) {
            const childData = {
                indexData: {
                    sessionDate: session.sessionDate,
                    customer1FirstName: session.customerInfo.customers.customer1.customerFirstName,
                    customer1LastName: session.customerInfo.customers.customer1.customerLastName,

            childData.objectID = childKey;
            console.log("PUSHING TO SESSIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", childData)
    return _sessions;
}).then((_sessions) => {
    console.log("ALL SESSIONS", _sessions.length, _sessions.slice(0, 5));

    .then(() => {
      console.log('Contacts imported into Algolia');
    .catch(error => {
      console.error('Error when importing contact into Algolia', error);


Hi there,

Glad you solved it!

1 Like