You are using the reduce properly. CouchDb reduces everything regardless of the keys. So what you are seeing is couchdb doing full reductions of modified nodes during index updates. This gives you the option of reducing across arbitrary key ranges.

-Damien

On Dec 23, 2008, at 8:53 PM, Simon Wex wrote:

I have been working on a few projects with couchdb, some of which are
in production so it came as a complete surprise today when I realized
I don't understand CouchDB's reduce implementation.

I have a map that emits a complex key and a count of 1 for each
document. From that I end up with a data set like so:

{"id":"89c285e3-109f-4ce8-9a11-f5219c6a0b36","key": [341235,"auctioned"],"value":1}, {"id":"ab37e07a-d19e-4ddb-b0ab-7da8b5e24a03","key": [341235,"auctioned"],"value":1}, {"id":"b5cf63e0-1892-41df-833b-84468365a08a","key": [341235,"auctioned"],"value":1}, {"id":"89c285e3-109f-4ce8-9a11-f5219c6a0b36","key": [341235,"teasered","125"],"value":1}, {"id":"3341b67a-c789-492e-a9e7-1a3a99540c59","key": [341235,"teasered","127"],"value":1}, {"id":"7e6d6077-619b-4cec-8cc2-d2e8779cdc15","key": [341235,"teasered","127"],"value":1}...

Now what I want from my reduce if the above was the only output is to
end up with a set like so:

{"key":[341235,"auctioned"],"value":3},
{"key":[341235,"teasered","125"],"value":1},
{"key":[341235,"teasered","127"],"value":2}...

I thought my reduce statement would have to look simply like so:

function(keys, values) {
 return sum(values);
}

Much to my surprise, when I didn't get the numbers expected, I did
some logging from my reduce function:

{
 "keys": [
   [[341263,"teasered","125"],"e13d8135-844e-4fec-bf3a-ed189359b30c"],
[[341263,"teasered","127"],"ae447f40-51cd-406e-8a65- f5a7f163d20e"], ...
 ],
"values": [1,1,...],
"rereduce": false
}

I thought that the reduce function was only called with results of the
same key. Essenially I think I was assuming that the functionality of
reduce when rereduce is true was always the case. Can someone help me
understand why a reduce function would get multiple keys for a single
reduce? Also, what should my reduce then look like?

Thanks for the help, Simon.

Reply via email to