I set out to create an app to help others learn the basic Polish conjugations. I wanted the application to have an exceptional search ability because we all learn language differently and we may remember parts of words, or even the sounds of them.

Algolia allowed me to index my database very quickly, it is extremely powerful. Elasticsearch is an alternative, but I wanted to develop a true “configless/serverless” experience with this application.

I created a firebase function that is executed via a url, this function will index my firestore collection/s. I then used the algolia client API to search on these indexes, again not having to obfuscate any important API or admin keys as no one is able to write to the algolia indexes without the admin key.

Let’s get started.

Step 1: Setup firebase

$ npm install -g firebase-tool $ firebase login $ mkdir firebase-functions && cd firebase-functions $ firebase init functions

Follow the rest of the CLI process and get your firebase functions project set up.

Step 2: Setup your project

In the firebase-functions project, install and save algoliasearch.

$ npm i algoliasearch --save

Step 3: Write your function

Note: You will need to upgrade from the spark plan to use functions that use external API’s.

Make sure that you have setup Algolia.

// functions/index.js const functions = require('firebase-functions');

const admin = require('firebase-admin');

const algoliasearch = require('algoliasearch');



const ALGOLIA_APP_ID = 'XXXXXXXX';

const ALGOLIA_ADMIN_KEY = 'XXXXXXXX';

const ALOGOLIA_INDEX_NAME = 'XXXXXXXX';



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



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

const arr = [];

admin.firestore().collection('COLLECTION_NAME').get().then(docs => {

docs.forEach(doc => {

const verb = doc.data();

verb.objectID = doc.id;

arr.push(verb);

});



const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);

const index = client.initIndex(ALOGOLIA_INDEX_NAME);



index.saveObjects(arr, (err, content) => {

if (err) res.status(500);



res.status(200).send(content);

});

});

});

Step 4: Deploy & execute the function

Now that you have written your firebase function it’s time to deploy and execute.

$ firebase deploy --only functions

Once you have successfully deployed your function, you will see a message similar to this:

Function URL ( firestoreToAlgolia ): https://us-central1-MY_PROJECT.cloudfunctions.net/ firestoreToAlgolia

Alternatively you can find the url it in your firebase project, under the functions link.

Copy and paste that URL into your browser and after a few seconds your data will have been indexed by algolia search.

Step 5: Configure the client

I created a Algolia “class” in my react project, and you can do this in another way if you wish.

// utils/algolia.js import algoliasearch from 'algoliasearch';



const ALGOLIA_APP_ID = 'XXXXXXXX';

const ALGOLIA_API_KEY = 'XXXXXXXX';



const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY, {

protocol: 'https:'

});

const index = client.initIndex('COLLECTION_NAME');



export default index;

I then use it in my React “Verbs” container

// containers/verb/index.js import React, { useState } from 'react';

import algolia from 'utils/algolia'; const getVerbs = (verbs, setVerbs, query) =>

algolia.search({ query, hitsPerPage: 50 }).then(res => setVerbs(res.hits)); const Verbs = ({ history }) => {

const [loading, setLoading] = useState(true);

const [verbs, setVerbs] = useState([]);

const [query, updateQuery] = useState('');

const [fetching, updateFetch] = useState(true);



if (fetching)

getVerbs(verbs, setVerbs, query).then(() => {

setLoading(false);

updateFetch(false);

});



return (

<>

<SearchBar updateQuery={updateQuery} updateFetch={updateFetch} />

</>

);

}; ...

And that is it, you now have an extremely powerful search component in your application, indexed when new data is added by only executing a URL.

Please leave a comment and share your experience! I would love to hear what you thought of the article and if you have any tips to share with me.