MitchSaver
MitchSaver

Reputation: 27

How to search for more than one value in Firebase

How do I search the firebase for "name" or "middle_name" or "surname" in the same search?

Firebase Database

enter image description here

//Firebase Search

public void searchPlaces(final String text){
       
       
    Query query = placesRef.orderByChild("name").orderByChild("middle_name").orderByChild("surname")
                   .startAt(text)
                   .endAt(text + "\uf8ff");

           query.addListenerForSingleValueEvent(new  ValueEventListener() {
               
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   for (DataSnapshot ds : dataSnapshot.getChildren()) {
                       list.add(ds.getValue(Places.class));

                   }
               }

               @Override
               public void onCancelled(@NonNull DatabaseError databaseError) {

               }
           });
           }

Upvotes: 0

Views: 302

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

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. For an example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase

In your case however, since you want to search for one value across multiple fields, you will need to perform a separate query for each field.

Upvotes: 1

Related Questions