Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase Query to find more than one value in different keys [duplicate]

I want to create a search function like facebook that help all user to search other user by more than one value under Users list in my application.

I read the example from here https://firebase.google.com/docs/database/admin/retrieve-data

here is my firebase : firebase

I able to search the the user from firebase by "fullname" . here is my application : my applicantion

usersRef = FirebaseDatabase.getInstance().getReference().child("Users");

    Query searchAllQuery = usersRef
            .orderByChild("fullname")
            .startAt(searchBoxInput.toUpperCase())
            .endAt(searchBoxInput.toLowerCase() + "\uf8ff" )

edited this is the code for my recycle view

    FirebaseRecyclerOptions<SearchUserList> options = 
             new FirebaseRecyclerOptions.Builder<SearchUserList>()
            .setQuery(searchAllQuery, SearchUserList.class).build();

I expected the output can be search by more that one value for example :

.orderByChild("fullname")
.orderByChild("username")
.orderByChild("country")
  1. when I key in USA , all user with value "usa" will show out.
  2. when I search for username "wei" ,users with value "wei" will show in the list
like image 284
qing Avatar asked Dec 14 '25 13:12

qing


1 Answers

Firebase doesn't support ordering by multiple queries.

Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query throws an error.

Try ordering by a simple query an then filter the data, sample:

Query searchAllQuery = usersRef
            .orderByChild("fullname")
            .startAt(searchBoxInput.toUpperCase())
            .endAt(searchBoxInput.toLowerCase() + "\uf8ff" )

searchAllQuery.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
        // filter your data here by username and country
        User user = dataSnapshot.getValue(User.class);
        if (user.getUsername().contains('wei')  || user.getCoutnry().contains('usa')) {
            // add the user to your list maybe?
        }
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});
like image 83
Yash Avatar answered Dec 16 '25 08:12

Yash