Android: Firebase Search Query not working properly

minikate :

I am fairly new to Android app development. I have created a simple search query that searches my firebase database by name (see code below):

private void firebaseEventSearch(String name) {
    //query to search database based on text in textbox - made case insensitive
    Query eventSearchQuery = eventRef.orderByChild("name").startAt(name.toUpperCase()).endAt(name.toLowerCase() + "\uf8ff");
    eventSearchQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
                Event event = eventSnapshot.getValue(Event.class);
                events.add(event);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

It works fine for the most part, however, it will display all database results when I type in the letters "A" or "B" - the search does not work for these two letters. Also, if I type in a search string where the first letter matches a database entry, it will display this database item, even if the rest of the string does not match it.

I would really appreciate if anyone could help me understand why this search query is not working properly.

Frank van Puffelen :

Firebase Database queries are case-sensitive. So when you call orderByChild("name") all nodes are ordered by their name property. If you look at an ASCII chart, you'll see that this will lead to this order for lowercase and uppercase characters:

A B C .... X Y Z a b c ... x y z

Now your query takes a slice of this data. If you're searching for A, the slice is:

A B C .... X Y Z a

So that's way more than you want.

If you want to allow case-indifferent search on Firebase, the most common (but not completely failsafe) way to implement it is to add an extra property that stores the name in an case-indifferent way. E.g. "name_uppercase": "ZUHRAIN". With that you can search on:

eventRef.orderByChild("name_uppercase").startAt(name.toUpperCase()).endAt(name.toUpperCase() + "\uf8ff");

Also see:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88420&siteId=1