/firebase-tutorials

How to perform full-text search in Firebase?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to perform full-text search in Firebase?

 

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.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022