/firebase-tutorials

How to search by text in Firestore?

Master text search in Firestore with our step-by-step guide—covering setup, basic queries, and advanced full-text search using Firebase Extensions and Algolia.

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 search by text in Firestore?

 

Step 1: Set Up Firebase Firestore in Your Project

 

First, ensure you have a Firebase project and Firestore set up. If you haven't done this yet, follow these steps:

  1. Go to the Firebase Console.
  2. Click on "Add project" and follow the prompts to create a new project.
  3. Inside the Firebase project, navigate to "Cloud Firestore," and click “Create database.” Choose the appropriate setup (test or production mode).

 

Step 2: Add Firebase to Your Application

 

Integrate Firebase SDK into your application. You'll use Firebase JavaScript SDK or the SDK of your respective platform (Android, iOS).

For a web application, add Firebase by including the following in your HTML:

<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js"></script>

Initialize Firebase in your JavaScript code:

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: "YOUR_API_KEY",
    authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_PROJECT_ID.appspot.com",
    messagingSenderId: "SENDER_ID",
    appId: "APP_ID"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Access Firestore
const db = firebase.firestore();

 

Step 3: Structure Your Firestore Collections

 

Decide how you want to structure your documents in Firestore. For example, suppose you're working with a collection named products, each with a description and name.

 

Step 4: Perform a Text Search with Firestore

 

Firestore doesn't natively support complex text search (like full-text search) easily. Still, you can perform basic exact matches or prefix matches using Firestore's querying capabilities.

Consider a simple search using where queries:

// Search for products where the name equals 'exampleName'
db.collection('products')
    .where('name', '==', 'exampleName')
    .get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            console.log(`${doc.id} => ${doc.data()}`);
        });
    })
    .catch((error) => {
        console.log('Error getting documents:', error);
    });

For more robust search capabilities, such as partial matches or indexing, consider using Firebase Extensions or integrating a third-party service like Algolia.

 

Step 5: Use Firebase Extensions for Full-Text Search

 

Firebase now offers extensions that provide more features. You can utilize the Algolia Search extension for advanced search capabilities:

  1. In your Firebase Console, click on "Extensions" available in the navigation pane.
  2. Find "Search with Algolia" and click on "Install".
  3. Follow the instructions to configure the extension with your Firestore collections.

With Algolia, you can enhance your text search capabilities with indexing and more complex querying.

 

Step 6: Test Your Firestore Text Search Implementation

 

Ensure to test the search functionality in different scenarios to confirm it works as expected. Open your application, input search parameters, and check if the output is as desired.

Developers may write test scripts specific to their framework to automate and assert if the search results meet their criteria.

 

Conclusion

 

While Firestore doesn't natively support full-text search, you can perform basic text queries with relative ease. For more advanced searching, consider using Firebase Extensions or third-party search services. Always test your implementation to ensure it meets your application's requirements.

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