1

Here are the various attributes of a person.

Database

I want to implement a search where the results come if any of the fields: specializationField, hospitalName or fullName have the same letters.

For example if I search 'sh', this person should appear in the field, because of the similar hospital name.

This is the code I am using to search only for fullName:

 FirebaseRecyclerOptions<DoctorHelperClass> options =
                new FirebaseRecyclerOptions.Builder<DoctorHelperClass>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("Doctor").orderByChild("fullName").startAt(s.toUpperCase()).endAt(s.toLowerCase()+"\uf8ff"), DoctorHelperClass.class)
                        .build();

        adapter = new DoctorsAdapters(options, FindDoctorActivity.this);
        adapter.startListening();
        binding.rvListDoctors.setAdapter(adapter);

Please help me out

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
Haneen
  • 51
  • 1
  • 7

2 Answers2

1

There is no support for OR conditions in Firebase Realtime Database. You will either have to perform multiple queries and merge the results client-side, or create a specialized field for performing this search.

But given your question, you may be looking for text search capabilities that are well beyond what Firebase Realtime Database handles. Instead of trying to shoehorn those requirements onto Firebase, I recommend using an additional (or even other) database for meeting your text search requirements.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

As @Puf said, you can't achieve it at Firebase Realtime Database but you can do it at client side which mean at the Android part.

First, you cannot use FirebaseUI which is you are currently using, instead you need to use https://firebase.google.com/docs/database/android/read-and-write#read_data

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // You have to make for each loop
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
             DoctorHelperClass doc = snapshot.getValue(DoctorHelperClass.class);
             //List them in an array
             docList.add(doc);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
    }
};
mPostReference.addValueEventListener(postListener);

Once you have added all the list of doctors. You can compare them using the arrayList.

You can do something like this.

private void searchDoc(final String inputDoc){
  boolean isFound = false;
  for (DoctorHelperClass doc in docList){
      if (doc.getFullName() == inputDoc && doc.getHospitalName() == inputDoc){
           isFound = true;
           //Do something if found
      }
  }
}

I hope you get the concept of it.

Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46