Query by multiple keys (and index for multiple keys)

1,562 views
Skip to first unread message

Naoki Shibata

unread,
Dec 17, 2014, 4:22:05 AM12/17/14
to fireba...@googlegroups.com
(First of all, we love firebase! Thanks for awesome service :)

Is there any way to set index for multiple keys so that we can query using multiple keys?

It looks like we can have index for 1 key, given this doc.

Eg. purchase history of EC

Imagine dataset like this:
{
  $key1: {
    "user_id" : 111,
    "item_id" : "aaa",
    "timestamp": $timestamp1
  },
  $key2: {
    "user_id" : 222,
    "item_id" : "bbb",
    "timestamp": $timestamp2
  }
}

Then, if we want to query by a combination of (user_id, item_id, timestamp) like this
-----
SELECT * from TABLE WHERE user_id=$user_id AND item_id=$item_id AND timestamp>=$timestamp;
-----
we usually have index (user_id, item_id, timestamp) in RDB.


Is it possible to query using multiple keys?
If yes, how can we have an index for multiple keys?

Don Barthel

unread,
Dec 18, 2014, 6:14:41 PM12/18/14
to fireba...@googlegroups.com
This suggestion is not from experience, just an idea, if multi-field indexing isn't supported.

Instead, could you combine the fields into a redundant single field and index on that?

For example:

{
  sex: 'male',
  age: 10,
  indexed: 'male..10'
}

{
  sex: 'female',
  age: 9,
  indexed: 'female09'
}

...etc.

Note that in 'indexed', the text fields must right padded with spaces to the longest length and numbers must be left padded with zeros so that different values of 'indexed' are comparable.

Jacob Wenger

unread,
Jan 4, 2015, 8:36:50 PM1/4/15
to fireba...@googlegroups.com
Hey Naoki,

Sorry for the delay in answering your question. Most of the team has been on vacation for the holidays. Thanks for your patience.

Querying on multiple keys at the same time is not currently supported. You have two other options in addition to what Don mentioned (which I hadn't even thought of, so good suggestion!):
  1. Do two separate queries (one for each key), and then manually find the intersection of the queries yourself.
  2. Do a single query on one of the keys and then manually filter the matches yourself based on the second key.
In both cases, you will probably download more data than is required, but until we add support for querying on multiple keys, those are you best bets.

Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/2691404a-6077-45db-824c-3152e7c79644%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages