-2

In an android app I am making I have implemented a search bar so a user may type in something in hopes of finding it in the database. I have is set up so that the result will come up, but only if it is spelled perfectly and completely. I would like to make it so it can bring up results as the user is typing, and even if it is spelled incorrectly and just similar. I have read about the possible built in query options with the firebase library, but they don't seem to support what I am looking for. Is there some way to relatively simply add this to my project? I understand predictive text searches are not simply created, but is there some library I can import?

dylan
  • 143
  • 1
  • 2
  • 11

2 Answers2

2

You can search for strings using

databaseReference.orderByChild('_searchLastName')
         .startAt(queryText)
         .endAt(queryText+"\uf8ff")
         .addValueEventListener ...
Martin De Simone
  • 2,108
  • 3
  • 16
  • 30
1

Building off of Martin's answer, if you want to show even if they are off by a little what I would do is filter.

databaseReference.orderByChild('_searchLastName')
     .startAt(queryText)
     .limitToFirst(10) //return 10 results
     ...

I assume you can convert to a list of Strings, this will give you all of the results starting with your your query and the next 9 which is more than what you want.

from here, you want to filter based on either contains or startsWith

public List<String> filter(List<String> listToFilter, String query){
    List<String> results = new ArrayList();
    for( String string : listToFilter){
        if(string.startsWith(query){//or contains
            results.add(string);
        }
    }
    return results
}