NetCod
NetCod

Reputation: 191

firebase queries and swift

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,

Upvotes: 0

Views: 514

Answers (1)

Brandon
Brandon

Reputation: 7976

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.

Upvotes: 1

Related Questions