How to Use Algolia Search in Firebase for Full Text Search
June 26, 2019
Firebase (https://firebase.google.com/) is a Backend-as-a-Service – BaaS product provided by Google. It allows developers to implement applications without server management. Performs API and datastore tasks for the application. Google offers many different services in this product such as Cloud Firestore. Cloud Firestore (https://firebase.google.com/products/firestore/) is a NoSQL database service that enables you to easily store, synchronize, and query data on a global scale for mobile and web applications. Firestore offers a powerful and flexible query structure but does not support full-text-search.
Full text search is one of the popular requirements for applications nowadays. A full-text search is a comprehensive search method that compares each word of the search request to each word in the document or database. For this challenge, there are different solutions with strong infrastructures such as ElasticSearch (https://www.elastic.co/) and Algolia (https://www.algolia.com/).
Let’s focus on how to solve full text search problem in Cloud Firestore using Algolia. Let’s prepare the codebase in stages.
Firebase & Algolia Setup
First of all, we have to complete Firebase and Algolia installations for the project. Install firebase-tool and login to firebase. After that initialize Cloud Functions directory. You can do this by completing the following steps.
npm install -g firebase-tool
firebase login
cd {ProjectDirectory}
firebase init functions
Now we have to make the necessary installations for Algolia. In the project directory, install and save algoliasearch.
npm i algoliasearch --save
Sync Data Using Cloud Function
Now let’s write a Cloud Function that synchronizes Algolia index with our Firestore collection. Thus, Algolia automatically indexes our data to use for full-text search. If we proceed with a simple example; Suppose you have a collection of “products” that we have stored at Firestore. In order to index these products, we need to synchronize these products with algolia every time a product is added. And also, update and delete operations must also be synchronized to ensure that the data is up-to-date and reliable. The Cloud Function required for these operations is shown below.
// functions/index.js
const functions = require("firebase-functions");
const algoliasearch = require("algoliasearch");
const admin = require("firebase-admin");
const ALGOLIA_ID = 'YOURAPPID';
const ALGOLIA_ADMIN_KEY = 'YOURADMINKEY';
const ALGOLIA_INDEX_NAME = "products";
const client = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);
admin.initializeApp(functions.config().firebase);
exports.onProductCreated = functions.firestore
.document("products/{productId}")
.onCreate((snap, context) => {
// Get the product document
const product = snap.data();
// Add an 'objectID' field which Algolia requires
product.objectID = context.params.productId;
// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(product);
});
exports.onProductUpdated = functions.firestore
.document("products/{productId}")
.onUpdate(async (change, context) => {
// Get the old product data
const oldProduct = change.before.data();
// Get the new product document
const product = change.after.data();
product.objectID = context.params.productId;
const index = client.initIndex(ALGOLIA_INDEX_NAME);
await index.partialUpdateObject(product);
});
exports.onProductDeleted = functions.firestore
.document("products/{productId}")
.onDelete((snap, context) => {
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.deleteObject(snap.id);
});
Thanks to this Cloud Function, Firestore and Algolia are synchronized. Now we can move to the final stage and it’s time to deploy Cloud Function.
Deploy Cloud Function
Deploy the function using the command below.
firebase deploy --only functions
If deployed successfully, we should see these three functions (onProductCreated, onProductUpdated, onProductDeleted) in the firebase dashboard Functions link.
As a result, Create update delete operations related to the product will trigger these functions and our data will remain synchronous. In the next step, we can search the full text from the algolia index in our application.