We have been doing development on a new project for several months on Couchbase and while we've done a number of views, we just recently introduced our first reduce.
The reduce function is operating on an index that is less than 100 rows, yet whenever I add a new document that matches the map, the reduce data is completely destroyed and returns zeros for all values. It stays like this for quite some time, 5 - 10 minutes, sometimes longer and then eventually starts outputting expected values.
Since the index is so small, I wonder what I might have done wrong. I could make it work with the data being stale - but going to 0 makes it unworkable. I'm hoping someone can look at my functions and tell me if I've written them in a way that won't perform.
The map:
function (doc, meta) {
if(meta.type == "json") {
if(doc.object == "product") {
emit([doc.account_id, doc.test], {active: doc.active, deleted: doc.deleted} );
}
}
}
The reduce:
function (keys, values) {
var out = {active: 0, inactive: 0, deleted: 0};
for(v in values) {
if (values[v].active == true && values[v].deleted == false) {
out.active++
}
if (values[v].active == false && values[v].deleted == false) {
out.inactive++
}
if (values[v].deleted == true) {
out.deleted++
}
}
return out;
}
We are running the latest version of Community edition - 2.2.0 (build-837). It's running in a dev environment with lots of hardware resources and very little usage. As a side note, other than this little snag, Couchbase has been great.
This may not be the root cause of your problem, but you haven't handled the re-reduce case in your reduce()
function - which can cause incorrect reduce results. See the Couchbase Developer Guide section on Understanding Custom Reduces and Re-reduce
I'd also suggest temporarily removing the reduce()
function to verify that the index operates as expected when when just using a map()
function.