Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform sql "LIKE" operation on firebase?

I am using firebase for data storage. The data structure is like this:

products:{    product1:{       name:"chocolate",    }    product2:{       name:"chochocho",    } } 

I want to perform an auto complete operation for this data, and normally i write the query like this:

"select name from PRODUCTS where productname LIKE '%" + keyword + "%'"; 

So, for my situation, for example, if user types "cho", i need to bring both "chocolate" and "chochocho" as result. I thought about bringing all data under "products" block, and then do the query at the client, but this may need a lot of memory for a big database. So, how can i perform sql LIKE operation?

Thanks

like image 487
yrazlik Avatar asked Mar 19 '14 12:03

yrazlik


People also ask

Can I use Firebase as SQL?

It is not possible to use Firebase in this way. Firebase is a real-time object store. It is not a SQL database and is not intended to be a replacement for one. It completely lacks mechanisms such as JOINs, WHERE query filters, foreign keys, and other tools relational databases all provide.

Is Firebase SQL or NoSQL?

The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.

Which is better SQL or Firebase?

Compared to Firebase, MySQL is better for multi-row transactions. On the other hand, Firebase is a satisfactory choice when it comes to managing huge data sets because NoSQL horizontally scales data and it is much faster than MySQL.

Does firestore use SQL?

Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections. Each document contains a set of key-value pairs.


1 Answers

Update: With the release of Cloud Functions for Firebase, there's another elegant way to do this as well by linking Firebase to Algolia via Functions. The tradeoff here is that the Functions/Algolia is pretty much zero maintenance, but probably at increased cost over roll-your-own in Node.

There are no content searches in Firebase at present. Many of the more common search scenarios, such as searching by attribute will be baked into Firebase as the API continues to expand.

In the meantime, it's certainly possible to grow your own. However, searching is a vast topic (think creating a real-time data store vast), greatly underestimated, and a critical feature of your application--not one you want to ad hoc or even depend on someone like Firebase to provide on your behalf. So it's typically simpler to employ a scalable third party tool to handle indexing, searching, tag/pattern matching, fuzzy logic, weighted rankings, et al.

The Firebase blog features a blog post on indexing with ElasticSearch which outlines a straightforward approach to integrating a quick, but extremely powerful, search engine into your Firebase backend.

Essentially, it's done in two steps. Monitor the data and index it:

var Firebase = require('firebase'); var ElasticClient = require('elasticsearchclient')  // initialize our ElasticSearch API var client = new ElasticClient({ host: 'localhost', port: 9200 });  // listen for changes to Firebase data var fb = new Firebase('<INSTANCE>.firebaseio.com/widgets'); fb.on('child_added',   createOrUpdateIndex); fb.on('child_changed', createOrUpdateIndex); fb.on('child_removed', removeIndex);  function createOrUpdateIndex(snap) {    client.index(this.index, this.type, snap.val(), snap.name())      .on('data', function(data) { console.log('indexed ', snap.name()); })      .on('error', function(err) { /* handle errors */ }); }  function removeIndex(snap) {    client.deleteDocument(this.index, this.type, snap.name(), function(error, data) {       if( error ) console.error('failed to delete', snap.name(), error);       else console.log('deleted', snap.name());    }); } 

Query the index when you want to do a search:

<script src="elastic.min.js"></script>  <script src="elastic-jquery-client.min.js"></script>  <script>     ejs.client = ejs.jQueryClient('http://localhost:9200');     client.search({       index: 'firebase',       type: 'widget',       body: ejs.Request().query(ejs.MatchQuery('title', 'foo'))     }, function (error, response) {        // handle response     });  </script> 

There's an example, and a third party lib to simplify integration, here.

like image 68
Kato Avatar answered Sep 21 '22 06:09

Kato