I am getting an error when trying to do a DISTINCT reduce that I got from here. I have reproduced this error on the beer-sample bucket, so this should be easy to reproduce. I have not seen any errors in the mapreduce_errors.txt
file, or anything that would lead me anywhere in the others. (If you would like me to search or post snippets of other files, please ask).
Running couchbase enterprise 4 beta, on Windows 2008 R2 (This also happened on the 3.0.1 community edition as well.).
Here is my map function (Using the beer-sample bucket, that ships directly with couchbase).
function(doc, meta) {
switch(doc.type) {
case "brewery":
emit(meta.id);
break;
}
}
Here is my reduce function:
function(keys, values, rereduce) {
return keys.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
This is the error:
reason: error (Reducer: )
Also an imgur of the view page if it helps: http://i.imgur.com/KyLutMc.png
The problem lies within your custom reduce function: you're not handling the case when it's being called as part of a re-reduce.
As per Couchbase documentation:
Bold formatting is mine, and the highlighted words are quite important: you should consider that sometimes, you'll receive the
keys
argument with a value ofnull
.According to the docs, you should handle the case when
rereduce
istrue
within yourreduce()
function, and you should know that in this case,keys
will benull
. In the case of yourreduce()
function, you could do something like this:Here, I'm firstly handling the re-reduce phase. For this I'm flattening the array of arrays that I'm receiving in the
values
argument and then I'm removing the duplicates that might have appeared after the merge.Then it comes your original code, which returns the
keys
argument array without duplicates.To test that this
reduce()
function actually works, I've used the followingmap()
function:This intentionally generates duplicates, which then are removed by the
reduce()
function.While this reduce works as a development view, it does not as a production view. The dataset must be too large so you have to implement the rereduce. This documentation should help http://docs.couchbase.com/admin/admin/Views/views-writing.html#reduce-functions