couchbase Reduce给出了不想要的结果

时间:2017-08-24 09:08:50

标签: couchbase couchbase-view

我有一个map函数,它返回如下结果:

{"total_rows":100995,"rows":[
{"id":"00001_372792","key":["00001","CADENCIER",0],"value":-0.1961035657664066},
{"id":"00001_372792","key":["00001","CADENCIER",0],"value":-0.1961035657664066},
{"id":"00001_386302","key":["00001","CADENCIER",0],"value":0.6934708647727543},
{"id":"00001_386302","key":["00001","CADENCIER",0],"value":0.6934708647727543},
{"id":"00001_386963","key":["00001","CADENCIER",0],"value":0.6922628824612621},
{"id":"00001_386963","key":["00001","CADENCIER",0],"value":0.6922628824612621},
{"id":"00001_387089","key":["00001","CADENCIER",0],"value":0.6919048724571887},
{"id":"00001_387089","key":["00001","CADENCIER",0],"value":0.6919048724571887},
{"id":"00001_387091","key":["00001","CADENCIER",0],"value":0.6919048724571887},
{"id":"00001_387091","key":["00001","CADENCIER",0],"value":0.6919048724571887},
{"id":"00001_387099","key":["00001","CADENCIER",0],"value":0.6921140124188077},
{"id":"00001_387099","key":["00001","CADENCIER",0],"value":0.6921140124188077},
{"id":"00001_387105","key":["00001","CADENCIER",0],"value":0.6921140124188077},
{"id":"00001_387105","key":["00001","CADENCIER",0],"value":0.6921140124188077},
{"id":"00001_387193","key":["00001","CADENCIER",0],"value":0.6936603115840247},
{"id":"00001_387193","key":["00001","CADENCIER",0],"value":0.6936603115840247},
{"id":"00001_387848","key":["00001","CADENCIER",1],"value":-0.29332158594360835},
{"id":"00001_387848","key":["00001","CADENCIER",1],"value":-0.29332158594360835},
{"id":"00001_388313","key":["00001","CADENCIER",1],"value":-0.0461553701861542},
{"id":"00001_388313","key":["00001","CADENCIER",1],"value":-0.0461553701861542},
{"id":"00001_388806","key":["00001","CADENCIER",1],"value":-0.04833054041013961},
{"id":"00001_388806","key":["00001","CADENCIER",1],"value":-0.04833054041013961},
{"id":"00001_388897","key":["00001","CADENCIER",1],"value":-0.25761199232338083},
{"id":"00001_388897","key":["00001","CADENCIER",1],"value":-0.25761199232338083},
{"id":"00001_435016","key":["00001","CADENCIER",1],"value":-0.037149057843773745},
{"id":"00001_435016","key":["00001","CADENCIER",1],"value":-0.037149057843773745}
...
]}

我想减少按键分组并返回每个键的值计数以及值的其他计算。

我这样做了:

function (key, values, rereduce) {

  var result = {};
  var ecartsSum;
  for(var i = 0; i < values.length; i++) {
    ecartsSum =+ values[i];
  }
  result.productsNumber = values.length;
  result.index = 100 + (Math.tan(ecartsSum/values.length)) * 100
  return result;
}

当我使用密钥["00001","CADENCIER",0]

请求视图时

我得到了这个结果:

{
  "productsNumber": 3,
  "index": null
}

这完全不是我期待的结果。

PS:我使用这些选项来选择:     connection_timeout = 6亿&安培; full_set =真安培;基团=真安培; inclusive_end =真安培;键=%5B%2200001%22%22CADENCIER%22,0%5D&安培;极限= 6&安培;减少=真安培;跳过= 0&安培;陈旧=假< / p>

1 个答案:

答案 0 :(得分:1)

并非给定键的所有值都一次传递给reduce函数。 MapReduce视图将处理数据的子集,减少每个子集并使用相同的reduce函数组合它们,直到处理完所有值。

您需要使用rereduce参数,以便该函数可以减少以前调用自身的输出。

来自Re-reduce Argument文档:

  

为了处理增量映射/缩减功能(即更新现有视图),每个函数还必须能够处理和使用函数自己的输出。这是因为在增量情况下,函数必须同时处理新记录和先前计算的减少。

尝试使用documentation

中的此示例
function(key, values, rereduce) {
  var result = {total: 0, count: 0};
  for(i=0; i < values.length; i++) {
    if(rereduce) {
        result.total = result.total + values[i].total;
        result.count = result.count + values[i].count;
    } else {
        result.total = sum(values);
        result.count = values.length;
    }
  }
  return(result);
}