Search code examples
firebasegoogle-cloud-firestore

Google Firestore: Query on substring of a property value (text search)


I am looking to add a simple search field, would like to use something like

collectionRef.where('name', 'contains', 'searchTerm')

I tried using where('name', '==', '%searchTerm%'), but it didn't return anything.


Solution

  • There's no such operator, allowed ones are ==, <, <=, >, >=.

    You can filter by prefixes only, for example for everything that starts between bar and foo you can use

    collectionRef
        .where('name', '>=', 'bar')
        .where('name', '<=', 'foo')
    

    You can use external service like Algolia or ElasticSearch for that.