1

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 first, the user should search them owing to attribute such as Title.

enter image description here


The posts which meet the requirement of the user should be displayed in the firebase-UI recyclerview.


This is my Firebase data structure:

enter image description here


I want the sample code that will create a Query which returns Id of convenient Posts such as ImgBRr2YFYfAu1WwENS1Ucj6jz13_1514813350760.


Please help me to solve the problem, any suggestions and ideas will be welcomed

Tarlan Ahad
  • 370
  • 4
  • 15
  • on which basis you want to search id from firebase ? – Aditya Jan 02 '18 at 13:13
  • The value of **author language title** should be the same the texts in the edit text fields – Tarlan Ahad Jan 02 '18 at 13:15
  • So you are taking input `author`, `language` and `title` from user and want this post id from firebase ? – Aditya Jan 02 '18 at 13:18
  • Yes, please, maybe more helpfully you can help me to create a query that will return ID according to similarity range for example from 100% similar to 80% similar. But may be :D – Tarlan Ahad Jan 02 '18 at 13:24
  • But this is not possible. You can perform only one query at a time on firebase. So, perform searching either by use author, language or title. – Aditya Jan 02 '18 at 14:29
  • Okay i have edited the question, could you help me to create autocomplete with Books title after Clicking the item of AutoComplete it will display all book with the Title of Written text. – Tarlan Ahad Jan 02 '18 at 14:52
  • 1
    Yup it can be performed easily. – Aditya Jan 02 '18 at 14:53

2 Answers2

3

Here is what you can do,

For search by language and then filter by title & author

public void search(String languangeSearchString, String title, String author){  
        FirebaseDatabase.getInstance().getReference().child("Posts").equalTo("language").startAt(languangeSearchString).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(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
                    }
                }           
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

}
mark922
  • 1,136
  • 2
  • 11
  • 20
1

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

Aditya
  • 3,525
  • 1
  • 31
  • 38