Skip to content Skip to sidebar Skip to footer

It Is Possible To Have A Contains Search Query In Firebase?

I tried this: Query firebaseSearchQuery = myRef.orderByChild('from').startAt(searchText).endAt(searchText+'\uf8ff'); but i want something like this Query firebaseSearchQuery = m

Solution 1:

For small datasets you can use the following code:

ValueEventListener valueEventListener = newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        List<String> list = newArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Stringfrom = ds.child("from").getValue(String.class);
            list.add(from);
        }

        for(String str : list) {
            if(str.contains(searchText)) {
                Log.d("TAG", "String found!");
            }
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
myRef.addListenerForSingleValueEvent(valueEventListener);

This solution is not recommended for large datasets because you'll need to download the entire node in order to make a search. In this case, Algolia or Elasticsearch are recommended.

If you intend to use Cloud Firestore, I recommend you to see my answer from this post. For more information, I also recommend you see this video.

Solution 2:

You need maybe this:

QueryfirebaseSearchQuery= myRef.orderByChild("from").equalTo(searchText + "\uf8ff");

EDIT

I guess it is not possible using firebase, it needs a special service in order to be done. A service that has to do with advanced searching (something like elastic search).

Post a Comment for "It Is Possible To Have A Contains Search Query In Firebase?"