Alon Shlider
Alon Shlider

Reputation: 1298

Firebase database doing an SQL "like" query with deeply nested childs

I have an edit text. I am watching the text changing at each letter and for each letter added I want to query a request to firebase, giving me the results that have this letters added. So I tried the following solution offered here -

How to do a simple search in string in Firebase database?

and it doesn't seem to work.

I have these JSON profiles in my database:


{
  "002xUGm1uRXDYc8JpEy7AKFWFyy2" : {
    "contest_uid" : "-LRoyQlTw5vo3CIE3tfb",
    "created_at" : 1543726522,
    "device_id" : "354020096008438",
    "email" : "[email protected]",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1543726522,
    "last_upload_started_at" : 0,
    "name" : "gold rush eren patym cash patym",
    "photoUrl" : "*"
  },
  "00IumwilishPwTgPUXX6GmveP5w2" : {
    "contest_uid" : "-LRoyQlTw5vo3CIE3tfb",
    "created_at" : 1543214441,
    "device_id" : "865758030661505",
    "email" : "[email protected]",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1543214441,
    "last_upload_started_at" : 0,
    "name" : "Khushal Chouhan",
    "photoUrl" : "*"
  },
  "00TRPNqSi1R41F4SUuhHOgHBdkN2" : {
    "contest_uid" : "-LO3Eg7zf1xh9RGDUy0e",
    "created_at" : 1541355818,
    "device_id" : "357149084088198",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1541355818,
    "last_upload_started_at" : 0,
    "name" : "Gagan Singh Gaur",
    "photoUrl" : "*"
  },


and here is what I have already tried to do -


mExploreSearchEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


      }

      @Override
      public void afterTextChanged(Editable editable) {
          if (editable.length() < 1) {
            mExploreSearchRecommendedProfiles.setVisibility(View.VISIBLE);
            fetchSixRandomProfiles();
          } else {
            mExploreSearchRecommendedProfiles.setVisibility(View.GONE);
            fetchNewListFromEditable(editable);
          }
      }
    });
  }

  private void fetchNewListFromEditable(Editable editable) {
    String text = editable.toString();
    Query query = FirebaseDatabase.getInstance().getReference().child("profiles").orderByChild("").startAt(text).endAt(text +"\uf8ff");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
          String users = dataSnapshot.getValue(String.class);
          Timber.tag("similar users").d(users); // <-- I have never got to this point 
        } else {
          Toast.makeText(App.getAppContext(), "snapshot does not exist", Toast.LENGTH_SHORT).show();
        }
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
        Timber.tag("similar users error - " + databaseError.getMessage());
      }
    });
  }


What I need is for every letter added, query the entire "profiles" table, if the name of the profile contains the entire charsequence from start then I need the name and the photoURL. What would be the best way to achieve this result ?

Upvotes: 0

Views: 91

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

Something like this should do the trick:

Query query = FirebaseDatabase.getInstance().getReference().child("profiles").orderByChild("name").startAt(text).endAt(text +"\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
      for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
        String users = snapshot.child("name").getValue(String.class);
        Timber.tag("similar users").d(users);
      }
    } else {
      Toast.makeText(App.getAppContext(), "snapshot does not exist", Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {
    Timber.tag("similar users error - " + databaseError.getMessage());
  }
})

Changes:

  • Use orderByChild("name") to order/filter the profiles on the value of the name property.
  • In onDataChange loop over the children, since a query can have multiple results.
  • You need to get the correct property value from each child snapshot, so snapshot.child("name").getValue(String.class)

What I haven't done yet:

  • Show the list of users in the search results. If you need that, have a look at creating your own adapter, and calling notifyDataSetChanged().

Upvotes: 1

Related Questions