What happens if I set query.groupLevel = 2 ?

111 views
Skip to first unread message

Johnny Caveman

unread,
Apr 16, 2014, 12:31:23 PM4/16/14
to mobile-c...@googlegroups.com
Hello everyone, I'm new to couchbase lite and a little confused the mechanism of groupLevel. 

Let's say if after map process, the document's key is an date-type-color array, and value is some concrete value need to be aggregated, how to write a REDUCEBLOCK equivalent to _count, which allows you to drill down when aggregating associated with groupLevel. Just like this.

Cheers

J. Chris Anderson

unread,
Apr 16, 2014, 12:47:56 PM4/16/14
to mobile-c...@googlegroups.com


On Wednesday, April 16, 2014 9:31:23 AM UTC-7, Johnny Caveman wrote:
Hello everyone, I'm new to couchbase lite and a little confused the mechanism of groupLevel. 

Let's say if after map process, the document's key is an date-type-color array, and value is some concrete value need to be aggregated, how to write a REDUCEBLOCK equivalent to _count, which allows you to drill down when aggregating associated with groupLevel. Just like this.


Group level works just the same in Couchbase Lite as it does in Couchbase Server or Apache CouchDB. What makes it hard to grasp for many people is it's simplicity...

So if you have [date, type, color] as a key structure, you can get a count of all items in a date range, or all items of a given type on a particular date (Assuming you aren't including hours in the date), or a count of the number of red trucks on that date.

If you want to count all red items or all with type=truck, you'll need an index with keys in a different order, maybe [type, color] or [color, type] depending on your app.

Hope that helps!

Jens Alfke

unread,
Apr 16, 2014, 4:35:45 PM4/16/14
to mobile-c...@googlegroups.com

On Apr 16, 2014, at 9:31 AM, Johnny Caveman <seln...@gmail.com> wrote:

Let's say if after map process, the document's key is an date-type-color array, and value is some concrete value need to be aggregated, how to write a REDUCEBLOCK equivalent to _count, which allows you to drill down when aggregating associated with groupLevel. Just like this.

Just write a trivial reduce block that counts rows. Setting the groupLevel will aggregate the keys together, and the reduce block will give you a count.
So if you set the groupLevel to 1, you’ll get a row for each date whose value is the number of items with that date.

—Jens

Johnny Caveman

unread,
Apr 17, 2014, 1:06:37 AM4/17/14
to mobile-c...@googlegroups.com
Ah, so the parameter of reduce( keys, values, re-reduce ) is based on each of grouped result, I see. Thank you for answering the beginner level question.

I tried to log the activity of reduce block and I found it runs only once for each document, no matter what the groupLevel is given. That's a bit different from my assumption, say I got 3 records (color, type) and 2 of them have the exact same key and the 3rd is totally different. My assumption is when groupLevel is 1, couch separates them into two groups and call the reduce function twice. But the logger says reduce is called 3 times although the final result is correct. I'm curious what's behind the scene makes the magic job ?

J. Chris Anderson

unread,
Apr 17, 2014, 7:31:40 PM4/17/14
to mobile-c...@googlegroups.com
One thing that took me a while to grasp is that you can't rely on the the keys (within a reduce range) being reduced in any particular order. I did the tests an on a large dataset it's off by ~5% because keys aren't always ordered in reduce. Many times the best answer is one of the built-in reduces like _count or _sum

Chris.

Jens Alfke

unread,
Apr 17, 2014, 8:51:57 PM4/17/14
to mobile-c...@googlegroups.com

On Apr 17, 2014, at 4:31 PM, J. Chris Anderson <jch...@couchbase.com> wrote:

say I got 3 records (color, type) and 2 of them have the exact same key and the 3rd is totally different. My assumption is when groupLevel is 1, couch separates them into two groups and call the reduce function twice. But the logger says reduce is called 3 times although the final result is correct.

Can you post an example?

If you set the groupLevel to 1, all the rows with the same color (first item of key) will be grouped together, and the reduce function will be called on them to generate a single value. The resulting row in the output will have key equal to [color] and value being the reduce output.
If you set the groupLevel to 2, the rows with the same first two items in the key are grouped together.

In Couchbase Lite, the number of calls to the reduce function will be the same as the number of groups in the result. But you shouldn’t count on this. CouchDB would call it more often on smaller numbers of rows, and Couchbase Lite may do that in the future as a memory optimization.

—Jens

(By the way, which platform are we talking about here, iOS or Android?)
Reply all
Reply to author
Forward
0 new messages