Jake Biesinger
Jake Biesinger

Reputation: 5828

Is it possible to create a Couchbase view that groups output without a reduce function?

In Couchbase or CouchDB, is it possible to group without an explicit reduce function? In my client code, I want the data to be given to me just as a reduce would receive it (assuming all mappers were used as input, even during a rereduce). Using group=true without a defined reduce function gives me the error:

$ curl http://127.0.0.1:8092/default/_design/testing1/_view/all?group=true
{"error":"query_parse_error",
 "reason":"Invalid URL parameter 'group' or 'group_level' for non-reduce view."}

I can add the identity reduce function:

reduce(keys,data) {return data;}

but Couchbase complains that I'm not actually reducing anything:

{"rows":[], "errors":[
    {"from":"local","reason":"{<<"reduce_overflow_error">>,
        <<"Reduce output must shrink more rapidly: Current output: '...'
    }]}

I'd sure like to get the complete reduction in my client.

Upvotes: 3

Views: 1701

Answers (2)

J Chris A
J Chris A

Reputation: 2168

The use of the "identity reduce" is a common pitfall in Couch-style map reduce. The Couch reduce system is designed for reduce functions that look more like:

function(keys, values) {
   return values.length;
}

The identity reduce will end up storing multiple copies of the map rows, in a non-indexed data structure.

It sounds like what you want is the unique set of keys, regardless of the reduction value. In that case, I would use the _count reduce function, since it is efficient and can run on any underlying data.

Upvotes: 0

smathy
smathy

Reputation: 27961

This not a technological limitation, it's a logical limitation. You cannot logically group results without some reduction of those results. This is analogous to the GROUP BY in SQL, you can't use that unless you also have some sort of aggregate function in your SQL query.

Upvotes: 1

Related Questions