0

This is my firebase model

Assume this script:

    private static DatabaseReference;
    mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");


    Query firebaseSearchQuery = mUserDatabase.child("service").orderByChild("serviceName").startAt(searchText).endAt(searchText+"\uf8ff");


    FirebaseRecyclerAdapter<Workers, WorkerViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Workers, WorkerViewHolder>(
            Workers.class,
            R.layout.search_result_layout,
            WorkerViewHolder.class,
            firebaseSearchQuery
    ) {
        @Override
        protected void populateViewHolder(WorkerViewHolder viewHolder, Workers model, int position) {
             viewHolder.setDetails(model.getName(), model.getEmail());
        }
    };

What will be the query for finding users email by searching child of child's value (hardware) ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MASYAM
  • 35
  • 1
  • 5
  • the above code wont work anw, since there is a push id between the reference and child("service") – Peter Haddad Jan 27 '18 at 10:18
  • Thanks for replying #Peter Haddad , I wanted to know is there any way to match with value of child of push id ? – MASYAM Jan 27 '18 at 10:25
  • Firebase Database queries evaluate each child node under the location where you run the query. But the property it evaluates must be at a fixed path under each child. So you can't query double nested children like you have. As Alex answered this typically means that you should denormalize the data, i.e. create a top-level list of all services. Also see my answer here https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Jan 27 '18 at 15:28
  • Thanks boss #Frank van Puffelen . I denormalized the services data and it worked :) – MASYAM Jan 29 '18 at 06:17

1 Answers1

0

There is no way to achieve this using this database structure.

First of all between Users node and service node there is another node, named userId, which does not appear in any reference. Between the service node and the child serviceName there is another node, serviceId node, which is the unique key generated by the push() method. I also don't see where are you looping to get those childrens. But these are not the actual problems.

In such cases as yours, you need to use a tehnique named in Firebase, denormalization and for that I recomend you see this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.

So in your case, you need to create another node that will host only the services, so you can simply create a query. Unfortunately, Firebase does not allow you to query after multiple fields, so this is your only choice.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193