0

I have a string for eg: "My name is John" stored in Firebase.

How would I query firebase so I can find all the posts in Firebase that have "John" ?

I can search for the first term in a string now using:

DataService.dataService.BASE_REF.child("Posts").child(selectedComment.commentKey).queryOrderedByChild("userComment").queryStartingAtValue(comment).queryEndingAtValue(comment+"\u{F8FF}").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 

where comment = "My"

I read about using Elastic search with Firebase but wanted to check if there was an easier way in Firebase before I looked at ElasticSearch/Flashlight for Firebase,

3
  • You could download all the posts and perform the search on Swift. Not the most elegant solution. . Anyway, Here on Firebase Documentation you can see search examples: firebase.google.com/docs/reference/js/firebase.database.Query Commented Jan 7, 2017 at 21:10
  • While an answer has been accepted, the issue is reliance on yet another library to do a substring (wildcard) search. You can do this yourself - it's not easier - but you control the code. See my answer the this question Wilcard query. In your case, if you are searching for whole words, create a parser that automatically parses the sentence and stores each word. Then substring searches are super quick. It's not a lot of code and disk space is cheap so it's another option. Commented Jan 8, 2017 at 14:51
  • The DIY solution is complicated by how firebase works. To search an arbitrary collection for a text match, you have to download the entire collection to the client. While with small amounts of data, I think doing client-side searching is certainly the most simple option. However, anything beyond a trivial collection size and the bandwidth/cpu constraints become non-trivial. In my experience, this point arrives much earlier than you'd think--i.e., it doesn't take a very large collection to become unreasonable. Commented Jan 8, 2017 at 21:24

1 Answer 1

1

Unfortunately, Firebase doesn't support searching thru content like that (in any language SDK). From a Google Groups Post in July '16:

As a company that understands search, we're also a company that understands using the best tool for the job. For fuzzy matching and contains, a NoSQL, realtime data store isn't the correct tool--these queries would be slow and scale poorly. BigQuery or ElasticSearch are the right tool for providing useful results in a scalable and robust manner.

Right now, this involves deploying a small node script to sync your search results with the realtime data, as explained in the article with the sample Flashlight lib. In the future, it will become more "effortless" as we add integrations between Firebase and Cloud products, particularly Cloud Functions and BigQuery interoperability.

BigQuery is, as I understand it, not specifically designed for user-facing search.

Elasticsearch (specifically, the Firebase plugin Flashlight) is a potential solution, but as you alluded to, it's an incredible amount of overhead (deploying/managing or renting an ES cluster, configuring the plugin, etc.). If content search is an important enough part of your app to justify that time/$, you may want to consider solutions beyond Firebase for your database needs, as it's by far one of the service's weakest areas.

In my opinion, you have a few options beyond Flashlight:

  1. Algolia, a Search-as-a-service provider, does offer integration with Firebase, but I've never used it & so can't offer much more than to say that it exists.

  2. Another alternative might be maintaining a collection of documents you want to search on another service, like AWS Cloud Search

  3. Depending on the stage of your project & your needs, consider other Backends-as-a-Service that support more in terms of querying. E.g., GraphQL-as-a-service backends, like Scaphold.io, Graph.cool, and Reindex are all built on SQL databases, and (I believe) all support multiple types of querying.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.