SG View with Reduce

32 views
Skip to first unread message

Todd Freese

unread,
Oct 11, 2016, 6:25:05 PM10/11/16
to Couchbase Mobile
What is the syntax for a map/reduce function for SG? (An SG view called from the SG REST API).

I have this view which works:

{
    "views":{
        "tags_for_scene":{
            "map":"function (doc, meta) { if (doc.type == \"tag\") { emit([doc.scene_id, doc.tagName], doc); } }"
        }
    }
}

I need to add a reduce function to it and can't seem to find the documentation on the correct syntax.

Todd

Todd Freese

unread,
Oct 25, 2016, 4:58:31 PM10/25/16
to Couchbase Mobile
Anyone have any thoughts on this? What is the syntax for adding a reduce function to the above map function? This is for a SG view called via the SG REST API.

T

Jens Alfke

unread,
Oct 25, 2016, 5:18:55 PM10/25/16
to mobile-c...@googlegroups.com

On Oct 25, 2016, at 1:58 PM, Todd Freese <to...@filmworkers.com> wrote:

Anyone have any thoughts on this? What is the syntax for adding a reduce function to the above map function? This is for a SG view called via the SG REST API.

You’re creating a Couchbase Server view, so the info should be in the Server docs.
Basically you just add a “reduce” key next to the “map” key, whose value is a string containing a JS reduce function.

—Jens

Todd Freese

unread,
Oct 26, 2016, 10:06:29 AM10/26/16
to Couchbase Mobile
Thanks Jens for the doc reference.

I have this working CBL view:

CBLView *view = [self.document.database viewNamed: @"jobs/uniqueDayNumbersForJob"];

if (!view.mapBlock) {

    NSString *const kSceneDocType = [Scene docType];

    [view setMapBlock: MAPBLOCK({

        if ([doc[@"type"] isEqualToString:kSceneDocType]) {

            NSString *jobID = doc[@"job_id"];

            NSString *sceneID = doc[@"_id"];

            NSString *dayNumber = doc[@"dayNumber"];

            if (jobID == nil) {

               jobID = @"";

            }

            emit(@[jobID, dayNumber, sceneID], dayNumber); // emit order is fixed because emit element is referred by index from calling method.

        }

    }) reduceBlock:REDUCEBLOCK({

        return @(values.count);

    }) version: @"4"]; // bump version any time you change the MAPBLOCK body!

}

CBLQuery *query = [view createQuery];

query.groupLevel = 2;

query.descending = NO;

NSString *myJobId = self.document.documentID;

query.startKey = @[myJobId];

query.endKey = @[myJobId, @{}];

return query;


And I am trying to reproduce that same view in a CB view like this:

{
    "views":{
        "unique_days_for_scene":{
            "map":"function (doc, meta) { if (doc.type == \"scene\") { emit([doc.job_id, doc.dayNumber, meta.id], doc.dayNumber); } }",
            "reduce":"function (key, values, rereduce) { return values.count; }"
        }
    }
}

Called this way:


I can confirm that the map function is working correctly. But the reduce and group_level are not. Am I missing something here?

Todd

ad...@couchbase.com

unread,
Oct 26, 2016, 2:22:22 PM10/26/16
to Couchbase Mobile
Todd,

Reduce should work when querying the view via Sync Gateway's Admin API, but isn't supported for requests over the public API.  This is due to security requirements for query over the public API - there are issues around the interaction between the security wrapping we apply to the view, and the format of the view response when a reduce function is specified.

If this isn't working for you over the admin API, can you file an issue with the details in the SG repo?

There's some additional documentation on the Sync Gateway views here:

There's an open issue for the addition of reduce support to the public API - https://github.com/couchbase/sync_gateway/issues/857.  If that's the gap you're hitting, any details you can add regarding your use case there would be helpful.

Thanks,
Adam

Todd Freese

unread,
Oct 26, 2016, 3:17:09 PM10/26/16
to Couchbase Mobile
Bingo! Thanks! Using the SG admin port solved the problem. Although it really sucks having to add the server side proxying/middleware to use the admin port. It just adds a lot of additional complexity.

I commented on the existing GitHub issue as to how important this issue is to us.

Thanks for your quick reply.

T
Reply all
Reply to author
Forward
0 new messages