Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase: implementing pattern searching in firebase

I know there is no WHERE clause in firebase. But how we can implement pattern searching in firebase as we can do in SQL using LIKE operator.

How we can implement following sql query in firebase?

SELECT * FROM Customers WHERE City LIKE 's%';
like image 849
Shakirullahi Logico Avatar asked Dec 23 '13 10:12

Shakirullahi Logico


2 Answers

The Idea

A near solution is to create an index of your data which is indexed by City (or whichever other field you'd like to search by) in a server-side NodeJS script.

This will not quite replicate the behavior of LIKE, however it will allow for easy querying by City name, which may eliminate the need for that behavior.

The Node Script

This is typically done via a simple server-side script written in NodeJS. Imagine a scenario where your user data was stored in /users/data. Your script would look like this.

var ref = new Firebase("<Your Firebase>.firebaseio.com");

// A Firebase ref where all our users are stored
var userDataRef = ref.child('users/data');

// A Firebase ref which is where we store our index
var byCityRef = ref.child("users/byCity");

// Then bind to users/data so we can index each user as they're added
userDataRef.on("child_added", function (snapshot) {

  // Load the user details
  var user = snapshot.val();

  // Use the snapshot name as an ID (i.e. /users/data/Tim has an ID of "Tim")
  var userID = snapshot.name();

  // Push the userID into users/byCity/{city}
  byCityRef.child(user.city).push(userID);

});

This script will create a structure like this:

{
  "users": {
    "data": {
      "Tim": {"hair": "red", "eyes": "green", "city": "Chicago"} 
    },
    "byCity": {
      "Chicago": {
        "-asd09u12": "Tim"
      }
    }
  }
}

The Client Script

Once we've indexed our data, querying against it is simple and can be done in two easy steps.

var ref = new Firebase("<Your Firebase>.firebaseio.com");
var userDataRef = ref.child('users/data');
var byCityRef = ref.child('users/byCity')

// Load children of /users/byCity/Chicago
byCityRef.child('Chicago').on("child_added", function (snapshot) {

  // Find each user's unique ID
  var userID = snapshot.val();

  // Then load the User's data from /users/data/{ID}
  userDataRef.child(userID).once(function (snapshot) {

    // userID = "Tim"
    // user = {"hair": "red", "eyes": "green", "city": "Chicago"}
    var user = snapshot.val();

  });
});

Now you have the near realtime load speed of Firebase with powerful querying capabilities!

like image 194
Abe Haskins Avatar answered Oct 05 '22 23:10

Abe Haskins


You can't. You would need to manually enumerate your objects and search yourself. For a small data set that might be ok. But you'd be burning bandwidth with larger data sets.

Firebase supports some limited querying capabilities using priorities but still not what you are asking for.

The reason they don't support broad queries like that is because they are optimized for speed. You should consider another service more appropriate for search such as elastic search or a traditional RDBMS.

You can still use firebase alongside those other systems to take advantage of its strengths - near real time object fetching and synchronization .

like image 27
Mike Pugh Avatar answered Oct 06 '22 01:10

Mike Pugh