I want to create a simple search in my app, but cannot find anything on interwebs about it, that's more recent than 2014. There must be a better way. There are startAt and endAt functions but they don't work as expected and are case sensitive. How do you guys solve this problem? How can this functionality still not exist in 2016?
-
19this question has been asked 4 years ago still waiting for firebase to add the full search capabilities. – indrajeet May 05 '20 at 03:20
-
I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-in-real-time-using-jetpack-compose-952abaab15c5) will definitely help. – Alex Mamo Jul 19 '22 at 06:42
11 Answers
In my case I was able to partly achieve a SQL LIKE in the following way:
databaseReference.orderByChild('_searchLastName')
.startAt(queryText)
.endAt(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
.
In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName
property from the database.

- 9,947
- 7
- 67
- 110
-
I have also looked for this topic in the whole documentation, but did not find anything about it. With some further research, I found out that that this is the "slimmest" solution to achieve the LIKE functionality without having to use elastic search. Of course if you need further, more advanced search capabilities, your main chance is by setting up a solution with elastic search. However in my case this solution was perfectly solving my needs in a great and simple way. – Francesco Dec 08 '16 at 10:18
-
-
17How can I get the result **Fred, Freddy, Frey** with search key: **re**? – Chi Minh Trinh Jun 21 '17 at 09:30
-
4That would not be possible. The solution above provides "Starts With" functionality, but it does not allow to have a full text search like in SQL Server. It you need full text functionality, then you have to implement a solution with elastic search. – Francesco Jun 21 '17 at 13:36
-
7This works well, at least for the starting strings. But searching within a string remains a fundamental issue with firebase. I feel it shouldn't be much of a problem to apply regular expression based search at the firebase core itself and just expose the functionality via the database Reference interface. – Akah Jul 08 '17 at 09:58
-
2Isn't `startAt()` enough for this? The docs say the `startAt()` returns all nodes where value is greater than or equal to the given one. What is the role of `endAt()` here? – Turkhan Badalov Jul 08 '17 at 22:03
-
1
-
3This is officially documented here: https://firebase.google.com/docs/database/rest/retrieve-data#range-queries – bjunix Sep 11 '17 at 18:24
-
@Joshi the original code was referring to an old version of AngularFire. I updated the answer, as with the newest version you do not need once(value). – Francesco Jun 24 '18 at 17:15
-
@TurkhanBadalov Great question. Say you're searching for banana and you have in your database apple and banana. If you just have start at and you search apple, you will get both results because the query doesn't "know" where to stop. – technoplato Dec 05 '18 at 21:53
-
@Francesco How do I use this for deeply nested databases? I have a database, which has a profiles child, that have child keys UID, each UID is a profile object that has a child "name", and I need to go and check through all "names" inside each profile - will this method work? and if yes - how do I use it properly? and what about if I actually get a result and I want also a imageURL that is a child of the profile object? – Alon Shlider Sep 24 '19 at 14:51
-
1. It's case sensitive. 2. It's not full text search. as @Francesco said 3rd party API elastic search would be good option for search with firebase. – indrajeet May 05 '20 at 03:16
-
`In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey"` is still possible without a `prefixed underscore _` – mustangDC Jul 31 '20 at 14:01
-
@mustangDC: the "_" is just part of my property name, therefore its name (with or without underscore) simply depends on how you designed your DB, But it does not bring any logic to the query (except the selection of the target property) – Francesco Aug 03 '20 at 10:37
Create two String variables
searchInputToLower = inputText.getText().toString().toLowerCase();
searchInputTOUpper = inputText.getText().toString().toUpperCase();
Then in the Query set it to:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Products");//Your firebase node you want to search inside..
FirebaseRecyclerOptions<Products> options =
new FirebaseRecyclerOptions.Builder<Products>()//the Products is a class that get and set Strings from Firebase Database.
.setQuery(reference.orderByChild("name").startAt(searchInputUpper).endAt(searchInputLower + "\uf8ff"),Products.class)
.build();
the "name" it's the node inside the Products Main Node.
the .startAt(searchInputUpper) & .endAt(searchInputLower + "\uf8ff") to make the search as contains all characters that typed in the inputText.getText() that you get.

- 89
- 1
- 5
finally I got it you can use where clause to get you result like SQL
LIKE keyword like%
or %like
syntax :
Firestore.collection(collectionName).orderBy(field).where(field, ">=", keyword.toUpperCase()).where(field, "<=", keyword.toUpperCase() + "\uf8ff").get()

- 671
- 7
- 21

- 71
- 1
- 2
Firebase is noSQL therefore it does not have searches built in like you'll find in SQL. You can either sort by values/key or you can equalto
https://firebase.google.com/docs/database/android/retrieve-data
You can find examples at the link above. That is the latest documentation for firebase.
If you are looking for SQL like searches. Then take a look at elastic search. But that will increase the complexity since you need a platform to put it on. For that i could recommend Heroku or maybe GoogleCloudServers
Here is a blog post about advanced searches with elastic search https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

- 303
- 2
- 6

- 1,360
- 5
- 18
- 38
-
It's sad, but elastic looks like the only answer. Thank you. I use my server for subscription validation anyway. – Zigmārs Dzērve Jul 27 '16 at 17:42
I my case used:
var query = 'text'
databaseReference.orderByChild('search_name')
.startAt(`%${query}%`)
.endAt(query+"\uf8ff")
.once("value")
In this way, searching by "test" I could get the records having "Test 1, Contest, One test" as value in 'search' property from the database.
-
1`startAt` and `endAt` in the instance of "test, testing, tes, contest, one test" if i search t, the return will be test, testing and tes – Lucem Apr 29 '18 at 06:11
This question might be old but there is a documented way of how to achieve this way, It is simple to implement. Quoted:
To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia. Consider a note-taking app where each note is a document:
Algolia will be part of your firebase functions
and will do all the searches you want.
// Update the search index every time a blog post is written.
exports.onNoteCreated = functions.firestore.document('notes/{noteId}').onCreate(event => {
// Get the note document
const note = event.data.data();
// Add an 'objectID' field which Algolia requires
note.objectID = event.params.noteId;
// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(note);
});
To implement the search, the best way is to use instant search - android
Sample Search Image: GIF

- 2,912
- 3
- 20
- 33
-
@PabloAlbaladejo the android code proves that you cant do a full string search unless you implement my code – Lucem Apr 19 '18 at 11:53
-
@PabloAlbaladejo its firebase functions, and its the only implementation he can make in relevance to his question thus my answer – Lucem Apr 20 '18 at 08:33
-
1Your quote is referring to firestore "Full Text Search", not firebase realtime database (there is a huge difference, and your answer mix both DB, and including Cloud Functions, so it is clearly offtopic). There is no option for this kinds of queries at firebase; you can read the official firebase blog https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html – Pablo Albaladejo Apr 20 '18 at 08:42
-
Does instant search interface directly with Firebase? I am a bit confused on how the cloud functions interface with instant search..... – tccpg288 Apr 29 '18 at 01:18
The feature you're looking for is called full-text search and this is something most databases (including Firebase) don't provide out-of-the-box because it requires storing the data in a format that's optimized for text search (vs optimized for filtering) - these are two different problem sets with a different set of trade-offs.
So you would have to use a separate full-text search engine in conjunction with Firebase to be able to do this, especially if you need features like faceting, typo tolerance, merchandizing, etc.
You have a few options for a full-text search engine:
- There's Algolia which is easy to get up and running but can get expensive quickly
- There's ElasticSearch which has a steep learning curve but uber flexible
- There's Typesense which aims to be an open source alternative to Algolia.

- 6,056
- 10
- 42
- 54
I don't know about the certainty of this approach but using the firebase version 10.2.6 on android, i get to do something like this:
firebaseDatabase.getReference("parent")
.orderByChild("childNode")
.startAt("[a-zA-Z0-9]*")
.endAt(searchString)
It seems to work well sometimes

- 1,389
- 14
- 19
-
Yeah I think. It still works as i am using firebase version 10.0.1 i think – Akah Nov 21 '17 at 23:49
-
let say i have a string "Apple smart phone" and my search term was smart, will it return "Apple smart phone" base on the query above. – suulisin Nov 22 '17 at 09:45
-
Not quite, you may need a different regular expression like: [a-zA-Z]{*}[ ][a-zA-Z] – Akah Nov 23 '17 at 10:05
-
1Firebase's query methods do not evaluate regular expressions. The above will only match when `childNode` *literally* matches `[a-zA-Z0-9]*` not when it starts with any number of alphanumeric characters. – Frank van Puffelen Mar 11 '18 at 16:02
Finally joined SO just to answer this. For anyone coming here from/for the python firestore.client here's a solution that seems to work for me. It's based on the accepted answer's concept but via the client rather than db.reference() and mixed with the answer from user12750908.
from firebase_admin import firestore
users = db.collection("users")\
.order_by("last_name")\
.where("last_name", ">=", last_name.upper())\
.where("last_name", "<=", last_name.lower() + "\uf8ff")\
.stream()
It works for the simple test I did, but I'll update my answer if I have issues with it later. And just a reminder, this is similar to
LIKE search%
and not
LIKE %search%.
Edit 1 I didn't see any tags for the question, but the title attribute mentions Android so this may not necessarily answer the question directly, but if you have a python API, this should work. I'm unfortunately not sure if there's an equivalent client/db separation in the Android version like there is in the Firebase Admin for Python. I didn't want to delete the answer since I hadn't seen any answers for firestore client during my search for a similar answer and hope it helps anyone else stumbling around.
Edit 09-03-2020 This works a portion of the time it seems. Most of the time I didn't seem to have an issue, but when I applied it to another project I was getting unexpected results. Long story short you may need to replicate how you save the data you're comparing against. For example, you may need to have a field to save the last_name in all caps and another field to save it in all lowercase, then you change the first where clause to compare last_name_upper and the second to compare last_name_lowercase. In my second project so far this seems to yield more accurate results so you may want to give that a try if the previous answer doesn't work well
EDIT 09-07-2020 Previous edit from 09-03-2020 is partially accurate. During my haste of thinking I had it fully resolved I completely forgot firebase doesn't let you use <, >, <=, >= across different fields. You may need to do two queries and merge them, but you'd probably still be reading more docs than you really intend. Doing the comparison against either the upper or lower version with the appropriate search term seems to give the original results expected from the original answer. For example:
.orderBy("last_name_upper")
.where("last_name_upper", ">=", this.searchForm.text.toUpperCase())
.where("last_name_upper", "<=", this.searchForm.text.toUpperCase() + "\uf8ff")

- 51
- 1
- 2
As firebase documentation, firebase doesn't support full text search. But to do that you can use third-party tools.
Check this link to learn more https://firebase.google.com/docs/firestore/solutions/search

- 43
- 1
- 8
If you're not satisfied with the orderBy
trick, but you don't want the added cost and complexity of algolia / elastic search, consider adding tags (array of strings) to your documents. If you're documents are tagged, you easily can build a nice combobox with autocompletion using javascript (example). Then you can use array membership to filter your documents based on the selected tag(s).

- 20,038
- 30
- 112
- 189