是否可以在 couchdb/cloudant 中将一组"ratings"映射/缩减为"relationships"?



我有"ratings"文档,记录用户对某项商品的评分,如下所示:

accountID: a1, itemID: i1, rating: 1
accountID: a2, itemID: i1, rating: 1
accountID: a3, itemID: i1, rating: 1
accountID: a1, itemID: i2, rating: 1
accountID: a2, itemID: i3, rating: 1

我想创建一个视图,显示哪些用户对同一项进行了评分,以及评分次数。从上面的数据可以看出,a1, a2, a3对同一个项目i1进行了评分,而i2, i3只是单独进行了评分。结果集应该如下所示:

accountID1: a1, accountID2: a2, numMatches: 1
accountID1: a1, accountID2: a3, numMatches: 1
accountID1: a2, accountID2: a3, numMatches: 1

这表明a1和a2都对同一个项目进行了一次评级(i1), a1和a3以及a2和a3(都是i1)也是如此。其他项目被忽略了,因为只有一个用户给它们打分。

是否可以在couchdb/cloudant中使用map/reduce实现这种转换?或者我必须在客户端进行计算,提取给定商品的所有评级并遍历每个帐户?

您可以解决这个问题的一种方法是在itemId+userId上建立索引,将评级作为值发出,然后使用_stats获得您想要的任何信息。这是你的设计文件:

{
  _id: "_design/count_shared_reviews",
  views: {
    "count_shared_reviews": {
      map: function(doc) {
        emit([doc.itemID, doc.accountID], doc.rating);
      }.toString(),
      reduce: "_stats"
    }
  }
}

然后你可以做http://localhost:5984/testdb1/_design/count_shared_ratings/_view/count_shared_ratings?reduce=true&group=true&group_level=1,它将按级别1(即项目ID)分组,给你:

{"rows":[
  {"key":["i1"],"value":{"sum":3,"count":3,"min":1,"max":1,"sumsqr":3}},
  {"key":["i2"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}},
  {"key":["i3"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}}
]}

您还可以按所有内容分组,在这种情况下,您将获得每个用户,每个项目的摘要(http://localhost:5984/testdb1/_design/count_shared_ratings/_view/count_shared_ratings?reduce=true&group=true):

{"rows":[
  {"key":["i1","a1"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}},
  {"key":["i1","a2"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}},
  {"key":["i1","a3"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}},
  {"key":["i2","a1"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}},
  {"key":["i3","a2"],"value":{"sum":1,"count":1,"min":1,"max":1,"sumsqr":1}}
]}

明白了吗?

相关内容

  • 没有找到相关文章

最新更新