Brinderjit
Brinderjit

Reputation: 23

Search child nodes in firebase database using android

Hello guys here is my firebase database:

Pic of my Firebase database

I want to get list of all medicines with particular symptoms.

Here is my code i.e what i have done

public void initializeMedicineListener(String node,String type,String value){
    mDatabase=mFirebaseInstance.getReference();
    Query query = mDatabase.child("Medicine").child("symptoms").orderByChild("name").equalTo("Neck pain");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            medicineList=new ArrayList<Medicine>();
            if (dataSnapshot.exists()) {
                for (DataSnapshot medicine : dataSnapshot.getChildren()) {
                    Medicine data = medicine.getValue(Medicine.class);
                    medicineList .add(data);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

But i am getting null results. Please guide me guys.Am i doing something wrong??

Upvotes: 2

Views: 1173

Answers (2)

Mohammad
Mohammad

Reputation: 739

You can't work with firebase database as relational databases .. instead of what you looking for .. you can create new node like this

medicineSymp
---neckpain 
-------medicine1ID
-------medicine1ID
-------medicine1ID

this way you could get all medicines much faster and easier

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

When you run a query at a location, Firebase check each child node at that location for the property that you order on and the range/condition you filter on.

mDatabase.child("Medicine").child("symptoms").orderByChild("name").equalTo("Neck pain");

So this checks the children of /Medicine/symptoms for their name property and only returns them if they have a value equal to Neck pain.

There are two problems with this:

  1. Your JSON doesn't have a /Medicine/symptoms. Instead you have a Medicine, where each child node has a symptoms node.
  2. Even if it did, your symptoms child doesn't have a value Neck pain. Instead you have an array, where each value may be Neck pain.

The closest you can now get to the query you want is:

mDatabase.child("Medicine").orderByChild("symptoms/0/name").equalTo("Neck pain");

This query returns medicines for which the first symptom name is equal to Neck pain. Firebase cannot perform a query across all array members to see if it contains a specific value in any position.

As usual with NoSQL databases: if you can't perform the use-case you want with your current data structure, you can typically change/expand your data structure to allow the use-case. And usually this is done by very directly mapping what you want to show on your screen to the structure in the database.

Your current data structures allows you to efficiently look up the symptoms (and other data) for a given medicine. That's great. It does however not allow you to efficiently look up the medicines for a given symptom. To allow that you can for example add a structure that maps each specific symptom back to its medicines:

symptoms
  "Neck pain"
    -L6hb2...bRb0: true
    -L6rW...Fuxkf: true

With this additional structure (known as an inverted index or reverse index) you can now look up the medicines for Neck pain by simple loading /symptoms/Neck pain.

For more on this approach to categorization, see my answer here: Firebase query if child of child contains a value

Upvotes: 4

Related Questions