Discover how to implement full-text search in Firebase using Algolia and Cloud Functions. Follow our step-by-step guide for setup, indexing, and executing searches.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Firebase Project
To begin, create and set up a Firebase project if you haven't already. Head over to the Firebase Console and follow the instructions to add a new project. You'll need to enable services such as Firestore or Realtime Database depending on where you want to store your data.
Step 2: Add Firebase to Your Application
Integrate Firebase with your web or mobile application. Follow the instructions in the Firebase console to add the correct SDKs and configurations to your app. Here's an example for a web application:
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-app.js"></script>
<!-- Add other Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-firestore.js"></script>
<script>
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
</script>
Step 3: Understanding Full-Text Search Functionality
Firebase natively does not support full-text search due to its NoSQL nature. However, you can implement full-text search either by using a combination of Firebase and a third-party service such as Algolia, or by leveraging Cloud Functions and a search service like Elasticsearch.
Step 4: Using Algolia with Firebase
One of the simplest ways to implement full-text search is to integrate Firebase with Algolia. Begin by creating an account on Algolia and creating an index for your search.
Step 5: Set Up Algolia with Firebase Functions
Install the Firebase CLI and initialize functions in your Firebase project:
npm install -g firebase-tools
firebase login
firebase init functions
Install Algolia's search client within your functions directory:
npm install algoliasearch
Step 6: Write Functions to Index Data
Within functions/index.js or functions/src/index.js, set up your Firebase functions to sync your Firestore data with Algolia. Here's an example:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
admin.initializeApp();
const ALGOLIA_APP_ID = "YOUR_ALGOLIA_APP_ID";
const ALGOLIA_ADMIN_KEY = "YOUR_ALGOLIA_ADMIN_KEY";
const ALGOLIA_INDEX_NAME = "YOUR_INDEX_NAME";
const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
const index = client.initIndex(ALGOLIA_INDEX_NAME);
exports.onProductCreated = functions.firestore
.document('products/{productId}')
.onCreate((snap, context) => {
const data = snap.data();
const objectID = snap.id;
return index.saveObject({
objectID,
...data
});
});
Step 7: Perform Searches
Use the Algolia client inside your app to perform searches. Install the Algolia client for your platform, then use it like this:
const algoliasearch = require('algoliasearch');
const client = algoliasearch('YOUR_ALGOLIA_APP_ID', 'YOUR_ALGOLIA_SEARCH_API_KEY');
const index = client.initIndex('YOUR_INDEX_NAME');
const query = 'search term';
index.search(query, {
attributesToRetrieve: ['attribute1', 'attribute2'],
hitsPerPage: 10
}).then(({ hits }) => {
console.log(hits);
});
Step 8: Testing and Maintenance
Test your full-text search to ensure it indexes and retrieves the data you want correctly. Regularly update your Algolia indices to reflect changes in your Firestore data.
This setup gives you a powerful text search capability using Firebase, Cloud Functions, and Algolia. Always ensure your implementation adheres to the latest best practices and security guidelines provided by Firebase and Algolia.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.