Skip to content Skip to sidebar Skip to footer

Android - Firebase Searching With Filtering

I'm developing an app that will help the user to search the books and download them which have been uploaded by other users as a form of the post. In order to download the books f

Solution 1:

Here is what you can do,

For search by language and then filter by title & author

publicvoidsearch(String languangeSearchString, String title, String author){  
        FirebaseDatabase.getInstance().getReference().child("Posts").equalTo("language").startAt(languangeSearchString).addChildEventListener(newChildEventListener() {
            @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
                 String id = dataSnapshot.getKey();
                if(dataSnapshot.child("title").exists() && dataSnapshot.child("author").exists()){
                    if(dataSnapshot.child("title").getValue(String.class).equals(title) && dataSnapshot.child("author").getValue(String.class).equals(author)){
                        //Here are the results
                    }
                }           
            }

            @OverridepublicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @OverridepublicvoidonChildRemoved(DataSnapshot dataSnapshot) {

            }

            @OverridepublicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });

}

Solution 2:

To perform searching on firebase-database, we commonly use orderBy(). Like

firebaseDatabase.getReference("Posts").child("postId")
                        .orderBy("authorId")

But it can't possible to use multiple orderBy() like,

firebaseDatabase.getReference("Posts").child("postId")
                     .orderBy("authorId")
                     .orderBy("title")     // this will throw an error
                     .orderBy("language") 

So you have to perform searching only on one child-node. For more info check this Query based on multiple where clauses in Firebase

Post a Comment for "Android - Firebase Searching With Filtering"