Error "Builtin _sum function requires map values to be numbers" with custom reduce function

I don’t have any views that use _sum function in design doc, even in other design docs and buckets. Only _count functions are present in some other views.

Please provide the full design documents and server logs (from all nodes).

Here design doc:

{“views”:{“MsgsBetweenUsers”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
if (doc.display.indexOf(doc.to) != -1){
var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
}
}”,“reduce”:"_count"},“UnreadMsgsBetweenUsers”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg” && doc.display.indexOf(doc.to) != -1 && doc.unread.indexOf(doc.to) != -1){
emit([doc.to,doc.from,doc.timestamp], null);
}
}”,“reduce”:"_count"},“UnreadMsgs”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg” && doc.display.indexOf(doc.to) != -1 && doc.unread.indexOf(doc.to) != -1){
emit([doc.to,doc.from,doc.timestamp], null);
}
}”,“reduce”:"_count"},“LastMsgs”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
if (doc.display.indexOf(doc.to) != -1){
var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
}
}”,“reduce”:“function(k,v,r){
if ®{
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}”}}}

I use 1 server for development and there were nothing in logs (for that time period).
UPD:
After some time error strangly disappeared and it worked well with both doc and manually generated json…
Then I’ve flushed bucket. Added some docs - worked well. Then some more. And finally error appear again. I’ve updated design doc above to current version (on which error is present). Logs are clean.

Hello. I’m running couchbase 2.0 on windows 7. When I try to query view with my custom reduce function couchbase somwhere tries to execute built-in _sum function.
Here is my map/reduce functions:

Map *:
function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
emit([doc.from,doc.to,doc.timestamp], doc);
}
if (doc.display.indexOf(doc.to) != -1){
emit([doc.to,doc.from,doc.timestamp], doc);
}
}
}

Reduce:
function(k,v,r){
if ®{
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}

Example doc:

{
“type”: “umsg”,
“display”: [
2,
1
],
“from”: 2,
“to”: 1,
“txt”: “message text”,
“timestamp”: 1363868459016,
“unread”: [
],
“id”: 6
}

This view shows last message from each user’s conversation partner.

When I try to query view via:

http://localhost:8092/mybucket/_design/dev_chat:umsg/_view/LastMsgs?stale=false&startkey=[1]&endkey=[1,{}]&group=true&group_level=2&reduce=true&connection_timeout=60000&limit=10&skip=0

it returns error:

error (Builtin _sum function requires map values to be numbers)

Why it tries to use "_sum " and where I don’t know. Aslo if I replace my reduce function with _count function it works well.

*Note: Yes, I need full “doc” in response.

UPD:
Also if I manually “copypaste” all doc fileds in map function like this:

function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“display”:doc.display,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:doc.unread,“id”:doc.id});
}
if (doc.display.indexOf(doc.to) != -1){
emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“display”:doc.display,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:doc.unread,“id”:doc.id});
}
}
}

All works well, no error.

Can you clarify what it means “logs are clean”? No errors messages or no content at all?
Perhaps you’re looking at the wrong place. Here’s how to collect all the logs:

http://www.couchbase.com/wiki/display/couchbase/Working+with+the+Couchbase+Technical+Support+Team

Even if there are no errors, events like for example saving a design document are logged at an info level. So it’s not expected at all to have empty logs.

That’s because you have another view in the same design document using _sum as its reduce function.
All views belonging to the same design document are updated and built simultaneously - querying one view of the design document (without stale=ok) will trigger an update of all views in the same design document.

FilipeManana wrote: The problem happens only when 2 or more views in the same design document have exactly the same map function and different reduce functions. That's I'm talking about :). Thanks for help.

PS: I thought that it could be nice, if couchbase developers implement views that can have one map function and many named reduce functions, i.e. create some reduce functions that count items and find unique items. Then via rest api query view with param reduce=_count or reduce=find_unique.

Yes, wrong place. I’ve send link to log archive in PM. Here are logs with this error:

[mapreduce_errors:error,2013-03-27T11:53:44.526,ns_1@192.168.1.50:<0.30799.0>:couch_set_view_group:-init_group/3-fun-3-:1675]Bucket peopleair, main group _design/dev_chat:umsg, error executing rereduce function for view `LastMsgs’
reason: Builtin _sum function requires map values to be numbers
input reductions: [[<<"{“type”:“umsg”,“from”:1,“to”:2,“txt”:“1 to 2 msg 24”,“timestamp”:1364311293116,“unread”:true,“id”:“6d1da303-6313-45f2-8f12-272b72120b47”}">>,
<<“14”>>],
[<<"{“type”:“umsg”,“from”:1,“to”:2,“txt”:“1 to 2 msg 24”,“timestamp”:1364311293116,“unread”:false,“id”:“6d1da303-6313-45f2-8f12-272b72120b47”}">>,
<<“34”>>]]

[couchdb:error,2013-03-27T11:53:44.526,ns_1@192.168.1.50:<0.30799.0>:couch_log:error:42]Set view peopleair, main group _design/dev_chat:umsg, writer error
error: {error,<<“Builtin _sum function requires map values to be numbers”>>}
stacktrace: [{couch_set_view_group,’-init_group/3-fun-3-’,7},
{couch_btree_copy,write_kp_node,2},
{couch_btree_copy,finish_copy_loop,1},
{couch_btree_copy,from_sorted_file,4},
{couch_set_view_updater,’-flush_writes/1-fun-9-’,3},
{lists,map,2},
{lists,map,2},
{couch_set_view_updater,flush_writes,1}]

[couchdb:error,2013-03-27T11:53:44.526,ns_1@192.168.1.50:<0.25178.0>:couch_log:error:42]Set view peopleair, main group _design/dev_chat:umsg, received error from updater: {error,
<<“Builtin _sum function requires map values to be numbers”>>}
[couchdb:error,2013-03-27T11:53:44.526,ns_1@192.168.1.50:<0.25090.0>:couch_log:error:42]Uncaught error in HTTP request: {throw,
{error,
<<“Builtin _sum function requires map values to be numbers”>>}}

Stacktrace: [{couch_set_view,get_group,3},
{couch_set_view,get_map_view,4},
{couch_view_merger,get_set_view,5},
{couch_view_merger,simple_set_view_query,3},
{capi_view,design_doc_view_loop,6},
{couch_httpd,handle_request,6},
{menelaus_web,loop,3},
{mochiweb_http,headers,5}]

Something strange is in section “input reductions:”. I can’t explain why it has array of 2 identical messages (in base there is only one doc with that id). So in some strange way it duplicates that message in reduce.

Also this error appeared after next manipulations:

  1. Add some docs.
  2. Query view with stale false.
  3. Add some more docs.
  4. Query again and receive error.

So when I create separate new design doc with view that contains identical map/reduce functions error not appear. All works as expected.

So if I need to get both list of items and their count it’s better to have one view for first task without any reduce function and another view with same map function and _count reduce function?

No. In that case you query with ?reduce=false, which queries the “map” side only of the view.
What you asked before was to able to specify a reduce function at query time.

I’ve send them via PM (if it works). Here is link Here

As I understand error is caused by this reduce function:

function(k,v,r){
if ®{
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}

If i replace it with another function (i.e. _count) it work well. And if I return this function it throw error again. But strange thing is that if I create separate (new) design doc and view with same map/reduce functions it works well. So reduce function seems to be correct.
UPD: More info.

As I said before if I create separate design doc with only one view error not appears. But if I add another view into that design doc, error appears. So here is that design doc’s code:

{“views”:{“test”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
if (doc.display.indexOf(doc.to) != -1){
var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
}
}”,“reduce”:“function(k,v,r){
if ®{
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}
if (v.length > 1){
var m = v[0].timestamp;
var mid = 0;
for (var i=1;i<v.length;i++){
if (v[i].timestamp > m){
m = v[i].timestamp;
mid = i;
}
}
return v[mid];
}
else {
return v[0] || v;
}
}”},“test2”:{“map”:“function (doc, meta) {
if (meta.type == “json” && doc.type == “umsg”){
if (doc.display.indexOf(doc.from) != -1){
var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
if (doc.display.indexOf(doc.to) != -1){
var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
}
}
}”,“reduce”:"_count"}}}

So as I understand that two views conflict between each other. Map functions of this 2 views are identical, but they have different reduce functions. Maybe index process takes results from first view and tries to apply reduce function of second view.

I’ve only seen your post with the backup link after my last reply.

I applied it and can now reproduce it. The problem happens only when 2 or more views in the same design document have exactly the same map function and different reduce functions.

I’ll address this soon and should be fixed by 2.0.2.
thanks for the data and report

As I said before this error can appear randomly after adding some docs. Have you tried to use my bucket snapshot from lastest message?

PS: Memtest show no errors.

Environment: Couchbase CE 2.0.0 Win7 x86_64 1 node in cluster.
Steps 2 reproduce *(now it throws error with “reason: unexpected”):

  1. Create empty bucket (no replicas).

  2. Create design doc (i.e. “test”).

  3. Create views with next map function and _count reduce function and custom reduce function.
    Map (same function for both views):

    function (doc, meta) {
    if (meta.type == “json” && doc.type == “umsg”){
    if (doc.display.indexOf(doc.from) != -1){
    var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
    emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    if (doc.display.indexOf(doc.to) != -1){
    var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
    emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    }
    }

    Custom reduce function:

    function(k,v,r){
    if ®{
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }

    So design doc will look like:

    {“views”:{“custom_reduce”:{“map”:“function (doc, meta) {
    if (meta.type == “json” && doc.type == “umsg”){
    if (doc.display.indexOf(doc.from) != -1){
    var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
    emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    if (doc.display.indexOf(doc.to) != -1){
    var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
    emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    }
    }”,“reduce”:“function(k,v,r){
    if ®{
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }”},“count_reduce”:{“map”:“function (doc, meta) {
    if (meta.type == “json” && doc.type == “umsg”){
    if (doc.display.indexOf(doc.from) != -1){
    var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
    emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    if (doc.display.indexOf(doc.to) != -1){
    var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
    emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    }
    }”,“reduce”:"_count"}}}

  4. For controlling that custom reduce function works well create separate design doc with only one view, that contains map function and custom reduce function, like that:

    {“views”:{“custom_reduce_single”:{“map”:“function (doc, meta) {
    if (meta.type == “json” && doc.type == “umsg”){
    if (doc.display.indexOf(doc.from) != -1){
    var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;
    emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    if (doc.display.indexOf(doc.to) != -1){
    var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;
    emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});
    }
    }
    }”,“reduce”:“function(k,v,r){
    if ®{
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }
    if (v.length > 1){
    var m = v[0].timestamp;
    var mid = 0;
    for (var i=1;i<v.length;i++){
    if (v[i].timestamp > m){
    m = v[i].timestamp;
    mid = i;
    }
    }
    return v[mid];
    }
    else {
    return v[0] || v;
    }
    }”}}}

  5. Add at least one doc into couchbase. It start throwing error just after adding first doc into couchbase. Doc example:

    DOC:
    {
    “type”: “umsg”,
    “display”: [
    1,
    2
    ],
    “from”: 1,
    “to”: 2,
    “txt”: “doc_1”,
    “timestamp”: 1364392897546,
    “id”: “43690bdc-436b-4b6c-be0b-1f9feb2b3af9”,
    “unread”: [
    2
    ]
    }
    META:
    {
    “id”: “chat:umsg:43690bdc-436b-4b6c-be0b-1f9feb2b3af9”,
    “rev”: “1-00000e33a0852c970000000000000000”,
    “expiration”: 0,
    “flags”: 0,
    “type”: “json”
    }

  6. Try to query view from second “control” design doc (that contains only one view) and enshure that it rerurns result without any errors:

    http://localhost:8092/errtest/_design/dev_test1/_view/custom_reduce_single?stale=false&startkey=[1]&endkey=[1%2C{}]&group=true&group_level=2&reduce=true&connection_timeout=60000&limit=10&skip=0

  7. Try to query view from first design doc and receive an error:

    http://localhost:8092/errtest/_design/dev_test/_view/custom_reduce?stale=false&startkey=[1]&endkey=[1%2C{}]&group=true&group_level=2&reduce=true&connection_timeout=60000&limit=10&skip=0

    I’ll add logs archive from last test shortly.
    UDP1:
    To get error about _sum function:
    After step 5: Add another doc (like a reply on message: from user 2 to user 1) like this:

    {
    “type”: “umsg”,
    “display”: [
    2,
    1
    ],
    “from”: 2,
    “to”: 1,
    “txt”: “doc_2”,
    “timestamp”: 1364394581583,
    “id”: “fc2d2a4e-2ef4-416e-acc1-8576b5b42cba”,
    “unread”: [
    1
    ]
    }

    UPD2: Created another bucket to reproduce with _sum error again. There is no constant doc count after which couchbase view start failing. At first I’ve added about 15 docs and all worked well. But then I’ve some new docs and it failed. I can backup that bucket if it help.
    UPD3: Bucket snapshot and logs from lastest test:

1
2

Haven’t seen anything yet that explains the _sum related error.
But, looking at the logs, your latest version of the design document is:

{
“views” : {
“UnreadMsgs” : {
“map” : “function (doc, meta) {\n if (meta.type == “json” && doc.type == “umsg” && doc.display.indexOf(doc.to) != -1 && doc.unread.indexOf(doc.to) != -1){\n emit([doc.to,doc.from,doc.timestamp], null);\n }\n}”,
“reduce” : “_count”
},
“UnreadMsgsBetweenUsers” : {
“map” : “function (doc, meta) {\n if (meta.type == “json” && doc.type == “umsg” && doc.display.indexOf(doc.to) != -1 && doc.unread.indexOf(doc.to) != -1){\n emit([doc.to,doc.from,doc.timestamp], null);\n }\n}”,
“reduce” : “_count”
},
“LastMsgs” : {
“map” : “function (doc, meta) {\n if (meta.type == “json” && doc.type == “umsg”){\n if (doc.display.indexOf(doc.from) != -1){\n var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;\n emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});\n }\n if (doc.display.indexOf(doc.to) != -1){\n var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;\n emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});\n }\n }\n}”,
“reduce” : “function(k,v,r){\n if ®{\n if (v.length > 1){\n var m = v[0].timestamp;\n var mid = 0;\n for (var i=1;i<v.length;i++){\n if (v[i].timestamp > m){\n m = v[i].timestamp;\n mid = i;\n }\n }\n return v[mid];\n }\n else { \n return v[0] || v;\n } \n }\n if (v.length > 1){\n var m = v[0].timestamp;\n var mid = 0;\n for (var i=1;i<v.length;i++){\n if (v[i].timestamp > m){\n m = v[i].timestamp;\n mid = i;\n }\n }\n return v[mid];\n }\n else {\n return v[0] || v;\n }\n}”
},
“MsgsBetweenUsers” : {
“map” : “function (doc, meta) {\n if (meta.type == “json” && doc.type == “umsg”){\n if (doc.display.indexOf(doc.from) != -1){\n var unread = (doc.unread.indexOf(doc.from) != -1) ? true : false;\n emit([doc.from,doc.to,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});\n }\n if (doc.display.indexOf(doc.to) != -1){\n var unread = (doc.unread.indexOf(doc.to) != -1) ? true : false;\n emit([doc.to,doc.from,doc.timestamp], {“type”:doc.type,“from”:doc.from,“to”:doc.to,“txt”:doc.txt,“timestamp”:doc.timestamp,“unread”:unread,“id”:doc.id});\n }\n }\n}”,
“reduce” : “_stats”
}
}
}

Which is not ok. The _stats reduce function only works if the corresponding map function emits numbers, and here you’re emitting documents (JSON objects), not numbers.
The log shows errors mentioning this:

[mapreduce_errors:error,2013-03-26T17:35:13.630,ns_1@192.168.1.50:<0.20692.32>:couch_set_view_group:-init_group/3-fun-3-:1650]Bucket peopleair, main group _design/dev_chat:umsg, error executing reduce function for view `LastMsgs’
reason: Builtin _stats function requires map values to be numbers

I tried you design document with a few documents based on the sample one you gave earlier, but wasn’t able to reproduce any issue other than _stats issue.

What I suggest in the meanwhile:

  1. Start with a clean bucket
  2. Add the design document
  3. Add 1 document, query the view with stale=false
  4. Add another document, and query the view with stale=false, repeat this step until you start getting the error.

If like this you’re able to reproduce the issue, please give me the list of documents you kept adding incrementally.

thanks

It’s normal it appears again. From the point a reduce function fails, indexing can’t progress further (unless map and/or reduce functions are updated/corrected).

I still haven’t received any logs.

Not in that way. I’ve proposed to predefine multiple reduce functions (in query time just select one), i.e. if you need in one app for one map to use _count, _sum, and something else. Now you need to create 2 views with same map and 2 different reduces. What I’ve proposed - the same way as it works now (to pre-compute all defined reduce functions), but on query just to select that I need in this moment.

Ok. That’s offtopic so let’s leave it :). Thanks for your answers.

I followed your steps on a single node, on Linux (shouldn’t matter).

Added the 2 ddocs, then the first doc, ran both queries: both succeeded, no errors. Then I added the second document, queried both views, and again no errors. Both queries returned exactly the same results.

In your previous logs I saw some weird stuff I can’t explain. It might be worth checking if your machine has good memory, by running a memory test tool such as memtest86+.

Thanks for the suggestion, but it wouldn’t work.

Couchbase views (like Apache CouchDB) are materialized views and it means that reductions are pre-computed, with the goal of providing fast querying (just streaming data from disk directly to client without computation).

Therefore adding such a query option ?reduce=whatever would imply at query time to apply the reduce/rereduce function against all emitted key/value pairs, not only this is CPU intensive, but also IO intensive, as it would need to go down to every leaf of the underlying btree to grab the emitted key/value pairs - instead of using the reduce value from the root node (for queries with ?group=false) or reductions from upper levels in the tree (queries with group_level=N or startkey/endkey).

Btw, got addressed today:

Will be included in 2.0.2.