3

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
qing
  • 67
  • 1
  • 10
  • You mean the output can be filtered by various values? – Constantin Beer Aug 24 '19 at 12:22
  • @ConstantinBeer yes, when we search for Fullname / nickname , user with that value will show out. From the example above , i can search TTT / sp to find out this user from my firebase – qing Aug 24 '19 at 13:37
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. So for example you could have a property `"country_username_fulname": "usa_sp_TT"` For a longer example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Aug 24 '19 at 14:00

1 Answers1

3

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) {

    }
});
Yash
  • 3,438
  • 2
  • 17
  • 33