Search code examples
firebasegoogle-cloud-firestorelowcode

Search for data in Firestore DB: Queries


I'm trying to search the 'author_name' field in my firestore database that 'CONTAINS' any of the keywords For example,

fields": {
        "Title": {
          "stringValue": "Fire in the gate"
        },
        "author_name": {
          "stringValue": "Stacey"
        },
        "story": {
          "stringValue": "fire was in the gate run run run"
        }
      }

in the given entry, if the term "Stace"`` or ``"Sta"`` or "cey" is entered it, should return the data that has the author_name field which CONTAINS the given word. I'm trying to implement this using the where() clause in firebase which helps in building complex queries.

Is there any other way to implement this?

Note: I'm using a low-code tool which mandates usage of REST API. I can't use any custom code in the app to implement this search. So, the search should be done on the firestore itself


Solution

  • Cloud Firestore doesn't natively support full-text search and instead recommends making use of a third-party indexing service that specializes in text search for NoSQL databases like Cloud Firestore.

    These indexing services can be connected with Firebase using Cloud Functions for Firebase and each service recommended by the documentation has a pre-built extension that you can deploy to connect that service.

    Note: Implementing full-text search will require the enabling of billing on your Firebase project and require connecting to services that also require billing. Most of the options listed below have free tiers which should cover light usage. At minimum, you will be charged a few cents a month by Google Cloud for hosting the containers that store your Cloud Functions. It is your responsibility to estimate your expected usage to determine the relevant billing costs and plan appropriately.

    The recommended services (at the time of writing) include:

    To deploy support for each service, you can either refer to their respective links above or view the code samples to define your own implementation.

    Because you want to expose this search capability as an API, you will also want to deploy a HTTPS Cloud Function to handle the client request and forward it over to the indexing service you selected.