How to make Firebase query search not case sensitive because you know Firebase provide exact match "==" with case sensitive. So we will solve this issue in the chapter in detail. We will use the Laravel PHP SDK for the firebase for all the queries stuff. Also we will see case insensitive sorting with Firebase orderBy using Firebase search ignoring case.
Now lets consider a simple example, you have 'Student' list and you want to search by name. So the main use of any search is that, you insert one or two characters and the system should search the most matching one, but that not the case in Firebase. Firebase is a case sensitive. Means string "Name" and "name" are two different values for Firebase. All Firebase developers are facing this issue, which is most basic necessity. So lets first understand, currently what is provided by Firebase. See the below example of Student List.
Now here you see, we have a Student named 'Avatar'. Now you have to search this student from whole list, so you start typing 'avatar', so atleast it should be match word ignoring its cases capital or lower. So do Firebase return has "Avatar" on search of "avatar". Let's check.
So, as per Firebase Console, we have option of "==", So we selected that and entered the keyword "avatar". You see the query above. So let's check what's the result?
So, it failed us. No result we got. So how Firebase works? Firebase works on exact match case sensitive. So if you have added the whole name "Avatar", then it would have shown you the result. But now what is use of writing whole name same to same and getting the result. Because it is making it totally useless on search of string. But don't worry the option hack you wouldn't find in Firebase Console, you can simply do it in your Laravel PHP code.
Let's begin the Laravel PHP code. You know how to fetch the Student list. Following is the reference of collection of Students
So hope you have got par the very necessity functionality of every system that Firebase was not supporting. And now you can implement search in your all list pages. And promised in previous chapter, we have completed the point "1) Case sensitive" challenge. Please read Chapter 16 and Chapter 17. Hope you are loving reading our chapters and let all know your doubts, feedback in the comment section below.
Now lets consider a simple example, you have 'Student' list and you want to search by name. So the main use of any search is that, you insert one or two characters and the system should search the most matching one, but that not the case in Firebase. Firebase is a case sensitive. Means string "Name" and "name" are two different values for Firebase. All Firebase developers are facing this issue, which is most basic necessity. So lets first understand, currently what is provided by Firebase. See the below example of Student List.
Now here you see, we have a Student named 'Avatar'. Now you have to search this student from whole list, so you start typing 'avatar', so atleast it should be match word ignoring its cases capital or lower. So do Firebase return has "Avatar" on search of "avatar". Let's check.
So, as per Firebase Console, we have option of "==", So we selected that and entered the keyword "avatar". You see the query above. So let's check what's the result?
So, it failed us. No result we got. So how Firebase works? Firebase works on exact match case sensitive. So if you have added the whole name "Avatar", then it would have shown you the result. But now what is use of writing whole name same to same and getting the result. Because it is making it totally useless on search of string. But don't worry the option hack you wouldn't find in Firebase Console, you can simply do it in your Laravel PHP code.
Let's begin the Laravel PHP code. You know how to fetch the Student list. Following is the reference of collection of Students
$student = app('firebase.firestore')->database()->collection('Students');
$keyword = 'avatar';
$student = $student ->orderBy('name','asc')
->startAt([strtoupper($keyword)])
->endAt([strtolower($keyword)."\uf8ff"]);
Here we used Orderby field 'name' in ascending order and used the startAt and endAt function of Firebase query. So this query orders all data by name in ascending starting at and ending at keyword 'avatar'. Now the main hack is the appended string "\uf8ff". This unicode is most last private unicode. Read more on google for this. So by this we would get all data containing 'avatar' keywords. Now further we have used strtoupper and strtolower. So it will start at 'AVATAR' and end at 'avatar' from keyword whatever the user has entered ("Avatar" or "aVatar" or AvataR").So hope you have got par the very necessity functionality of every system that Firebase was not supporting. And now you can implement search in your all list pages. And promised in previous chapter, we have completed the point "1) Case sensitive" challenge. Please read Chapter 16 and Chapter 17. Hope you are loving reading our chapters and let all know your doubts, feedback in the comment section below.
5 Comments
Let us know your doubts here.
ReplyDeletehow we can implement this in android
ReplyDeleteThanks Sanjoo for reaching out us.
DeleteYou can take reference from our blog. The query structure is almost same for android. Use https://firebase.google.com/docs/firestore/quickstart#java for the exact query in android.
Hope it helps. Please follow us and share. It helps us.
Wouldn't it also match Bvatar doing it like this?
ReplyDeleteHi Gijs Beijer
DeleteThanks for reaching out us.
Yes, "Bvatar" also be searched, but it would be after avatar.
But for accuracy and Full text search, we recommend to use algolia search. We are writing a chapter on that and usage of cloud function https://laravelwithfirebase.blogspot.com/2020/09/chapter-22-how-to-write-trigger-firebase-cloud-functions.html
Always open for feedbacks and suggestion.