couchdb地图/缩小视图:仅计算最新商品 [英] couchdb map/reduce view: counting only the most recent items
问题描述
我有以下文件.关键字的时间戳位置.
I have the following documents. Time stamped positions of keywords.
{
_id: willem-aap-1234,
keyword:aap,
position: 10,
profile: { name: willem },
created_at: 1234
},
{
_id: willem-aap-2345,
keyword:aap,
profile: { name: willem },
created_at: 2345
},
{
_id: oliver-aap-1235,
keyword:aap,
profile: { name: oliver },
created_at: 1235
},
{
_id: oliver-aap-2346,
keyword:aap,
profile: { name: oliver },
created_at: 2346
}
每个配置文件查找最新的关键字.名称可以通过以下方式完成:
Finding the most recent keywords per profile.name can be done by:
map: function(doc) {
if(doc.profile)
emit(
[doc.profile.name, doc.keyword, doc.created_at],
{ keyword : doc.keyword, position : doc.position, created_at: doc.created_at }
);
}
reduce: function(keys, values, rered) {
var r = values[0];
for (var i=1; i<values.length; i++)
if (r.created_at < values[i].created_at)
r = values[i];
return r;
}
然后使用
reduce : true,
group_level : 2,
startkey : [aname],
endkey : [aname,{}]
这为我提供了名称为aname的配置文件的最新文档.
This gives me the most recent documents for the profile with name aname.
但是现在我想计算每个关键字的所有最新文档,并对位置求和.我只能尝试仅使用map/reduce来解决这个问题.
But now I want to count all most recent documents per keyword, and sum the positions. I cannot get my head around this trying to do it with map/reduce only.
我的用户案例是:
- 根据个人资料,用户和关键字查找最新文档
- 计算每个关键字的唯一profile.name的数量
- 对每个关键字的最新文档的位置求和
使它起作用的唯一方法是使用以下列表函数:
The only way I can make it work is using the following list function:
function(head, req) {
var row;
var counts = {};
while (row = getRow()) {
var v = row.value;
var k = v.keyword;
if (v.position) {
if (!counts[k])
counts[k] = {
position : 0,
count : 0
}
counts[k].position += v.position;
counts[k].count++;
}
}
return JSON.stringify(counts);
}
有人能想到一种更好的方法,仅使用map/reduce吗?
Can anyone think of a better way to do this, using map/reduce only?
谢谢
推荐答案
某些部分的含义仍然有些模糊(例如,什么是位置"?).
The meaning of some parts are still a bit cloudy (for example, what is a "position"?).
但是从纯粹的形式上看,您的列表似乎在keyword
上创建了索引,而地图在[profile, keyword, timestamp]
上创建了索引.
But from a pure formal point of view, it seems that your list creates an index on keyword
while your map created an index on [profile, keyword, timestamp]
.
如果您确实需要不同的索引,则需要几个映射,每个索引一个.唯一的例外是,当您已经在[a,b,c]
上具有地图时,可以更改组级别"并获取其他两个索引:[a,b]
和[a]
.
If you really need different indexes then you need several maps, one per index. The only exception is when you already have a map on [a,b,c]
, you can change the "group level" and get two other indexes: [a,b]
and [a]
.
这篇关于couchdb地图/缩小视图:仅计算最新商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!