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

tehfailsafe picture tehfailsafe · Oct 4, 2017 · Viewed 62.3k times · Source

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.

Answer

Ankit Prajapati picture Ankit Prajapati · Jun 29, 2019

I agree with @Kuba's answer, But still, it needs to add a small change to work perfectly for search by prefix. here what worked for me

For searching records starting with name queryText

collectionRef.where('name', '>=', queryText).where('name', '<=', queryText+ '\uf8ff').

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText.