12

I wanna pull information from a Firebase Database but it's a case sensitive query, and i don't want it to be case sensitive, is there a way to make the query NOT case sensitive in Android Studio ,Here is the query code

DatabaseReference SearchRef = (DatabaseReference) FirebaseDatabase.getInstance().getReference().child("Child1").orderByChild("Child2").startAt(searchText).endAt(searchText + "\uf8ff"); // \uf8ff is a white space
    Query Searchquery = Searchref;
    Searchquery.addValueEventListener...
captindfru
  • 233
  • 1
  • 3
  • 13

1 Answers1

22

When using Firebase Database, that is the much you get to from orderByChild() function. It returns data either ascending or descending order and you cannot customize your result with extended queries.

However, You can try a something else that may be a bit more expensive. Get all the children you want as an array, saving Child2 and its key. You can then change the string to upper case or lower case and access the desired result using the key.

The reference result would be

FirebaseDatabase.getInstance().getReference("Child1").child(key);

EDIT

To add on your logic, If you want "Dave" or "dave" to return Dave and dave You can edit your query to startAt(text.toUppercase) and endAt(text.toLowerCase+ "\uf8ff"). This will return DAVE, Dave, dave etc

Disney Program
  • 310
  • 2
  • 9
  • Great answer, but the query search depends on something a user puts in ie an edit text, and i want the query search to return Child2 without having to worry about case sensitivity at all, let's say there are 3 child2 strings in the database, the first is "Dave" the second one is "dave" and the third is "Sam", if the user searches for "D" the query will only return "Dave", i want it to return "dave" too – captindfru Jun 12 '18 at 08:30
  • Oh, I get you more. Am editing the answer – Disney Program Jun 12 '18 at 08:33
  • Quick question, the `startAt(text.toUppercase)` will it return values with spaces too or do i have to add `"+ \uf8ff"` – captindfru Jun 12 '18 at 08:41
  • It wil return anything starting with that name. eg "Dave stone". Note this is not the official way to search in Firebase and it is better to use Firestore. – Disney Program Jun 12 '18 at 08:43
  • Ah, i understand, Thanks your answer is amazing , and yes i know but i'v structured my whole application on firebase database, and changing now will take a lot of time, which i don't have much of – captindfru Jun 12 '18 at 08:46
  • 25
    If I don't miss anything, then if you search between D and d + "\uf8ff", you get Dave and dave, but you also get everything starting with E, F, G, ..., Z, a, b, c... – Yossi Nov 18 '18 at 09:04
  • 1
    Experiencing the same thing^ – David Chopin Feb 17 '20 at 18:58
  • Another way would be to store a lowercase copy of child2 in child3 at the time of saving the object in database and then always convert your EditText query to lowercase and always query child3 instead of child2. – Shahood ul Hassan Jul 04 '20 at 14:49
  • 6
    cant believe firebase hasnt thought of this – SuperUberDuper Jul 11 '20 at 09:55