Cloudant搜索索引选择性查询

时间:2016-08-26 09:20:19

标签: couchdb cloudant nosql

您好我有以下用例,假设我有一个包含以下字段的Cloudant文档:

{
  public: false, // Visibility of the doc
  permissions: [1,2,3] // List of user IDs that are allowed view access to the doc
}

只有特权用户(如权限字段中所述)才具有访问权限,或者如果其公共(public:true),则每个用户都可以访问它。

当我尝试编写搜索索引以检索当前用户有权访问的所有文档时,我的问题就出现了。我的搜索索引将如下所示:

function (doc) {
    if (!doc.public) {
        doc.permissions.forEach(function(entry) {      
            index("user_id", parseInt(entry));
        });
    } else {
        index("user_id", ???); // If its public document, how should I index it?
    }
}

它在public设置为false的doc上运行良好,但我的搜索索引无法返回public设置为true的文档。我的搜索查询看起来像这样:

q=user_id:1

任何建议,因为我真的需要在Cloudant层而不是在我的控制器层上实现它。

1 个答案:

答案 0 :(得分:1)

根据您的描述,我发现观点更适合这种情况。如果您绝对需要查询搜索,我可以尝试找出另一种解决方案。

首先,我会做一个像这样的映射函数的视图:

function(doc) {
    if (doc.public) //If it's public, we will emit the magic ID *. Feel free to use any other key that will be unique.
        emit('*');
    else if (doc.permissions && Array.isArray(doc.permissions)) //We valid the permission property
        for (var i in doc.permissions)
            if (doc.permissions[i])
                emit(doc.permissions[i]);
}

然后,当我查询获取用户允许的文档时,我只是查询这样的视图:_view/byUserId?keys=['myUserId','*']

对于每一行,您将拥有 id 属性中允许的文档的ID。

相关问题