9

I have implemented firebase realtime database in my android app, everything is fine. I am trying to add a a search function and I am able to search the database with only starting word match. Below is my database snapshot:

enter image description here

I am querying for movieName. Right now I am able to search if the query string is Pets/Pe/pet. But when I search for Animals, I get zero results. So, basically what I am looking for is searching the database with query text anywhere in the string. ie., I should be able to search Animals/and/pets and should get the results which contain the search query.

Below is my code so far.

mDatabaseReference.child("recent").getRef().orderByChild("movieName").startAt(query)
                    .endAt(query + "\uf8ff")
Darshan Gowda
  • 4,956
  • 3
  • 19
  • 27
  • 1
    Take a look at [this example](https://github.com/firebase/functions-samples/tree/master/fulltext-search). Full text search becomes extremely easy with Algolia and Cloud Functions for Firebase. – Rohan Stark Sep 17 '17 at 07:24

2 Answers2

3

To seach for a string within a String please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference animalsRef = rootRef.child("Animals");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Boolean found;
        String search = "Animals";
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String movieName = ds.child("movieName").getValue(String.class);
            found = movieName.contains(search);
            Log.d("TAG", movieName + " / " + found);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
animalsRef.addListenerForSingleValueEvent(eventListener);

As you see, we query the database for all the movie names and then search for the desired word within the movieName.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 3
    You should add that this is client side filtering, and you're essentially not using the database to search; you're instead iterating over the entire database and filtering client side. This maybe an applicable solution to some cases, but not to other (my case involves many many billions of bytes, and its not practical to apply client side filtering). – Pedro Rodrigues Nov 30 '18 at 14:04
  • 1
    @PedroRodrigues If you consider at some point to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), you might also try [Algolia Search](https://stackoverflow.com/questions/49596610/is-it-possible-to-use-algolia-query-in-firestorerecycleroptions/49607796). As far as I know, it works great also with Firebase realtime database. – Alex Mamo Nov 30 '18 at 14:07
  • Yep, thanks for point that out. I guess its obvious I'm looking for some server side filter. I'm digging my way into the Algolia thing. Thanks, very helpful. – Pedro Rodrigues Nov 30 '18 at 14:41
0

First of all check the exact child and initialize the Firebase as like. Have a look at this answer, https://stackoverflow.com/a/34537728/5746625

TheSwiftGuy77
  • 656
  • 1
  • 9
  • 25