4

I am porting an app over from Parse to Firebase and I used to be able to query using matchesRegex and putting the search into "(?i)\(searchBar.text!)". By doing this, the user could search "p" and the database would return (for example) "person", "People", "pot", etc.

I can't seem to find a way to query like this using Firebase that allows a user to search part of a String and Firebase looks for the substring within the database.

I am using:

databaseReference.child("Schools").queryOrderedByKey().queryEqualToValue("(?i)\(searchBar.text!)").observeSingleEventOfType(.Value, withBlock: { (snapshot) in

    for childSnapshot in snapshot.children {

        print("SANP \(childSnapshot)")

    }

    }) { (error) in

        print(error)

    }

If I do this, nothing is printed. I know the code above works if I search for a specific value that is character-for-character the same as it is in the database.

How can I search using parts of a String within the Firebase database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • There is no built-in support for regular expression or LIKE "%...%" matching in Firebase queries. See http://stackoverflow.com/questions/38538863/firebase-query-containing-value, http://stackoverflow.com/questions/38473967/is-there-a-way-to-do-regex-query-or-return-results-that-contain-specific-text-in, http://stackoverflow.com/questions/10559191/firebase-and-indexing-search, http://stackoverflow.com/questions/22506531/how-to-perform-sql-like-operation-on-firebase, http://stackoverflow.com/questions/38566949/does-firebase-provide-us-all-queries-like-parse-database-for-android – Frank van Puffelen Sep 21 '16 at 04:00
  • So basically if your user types some random letters you want to show all the schools who have those letters in their school name, right(something like google search)? – Dravidian Sep 21 '16 at 09:43
  • @Dravidian Exactly! Like a google search. Or, say you are searching a person on Facebook, if you type in an A, it will show all friends that have an a or A in it. – Dan Levy Sep 21 '16 at 12:55
  • @DanLevy what you are asking is called a recommendation engine. Its kinda of Machine Learning (Artificial Intelligence:https://www.quora.com/How-exactly-is-machine-learning-used-in-recommendation-engines) if you are looking for big chunk of DB like Google, But for small database i can give you a solution But efficiency might be a problem there... – Dravidian Sep 21 '16 at 13:52

0 Answers0