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 :

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

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")
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) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With