Cloudant 搜索索引选择性查询

Cloudant search index selective query

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

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

只有特权用户(如权限字段中所述)才能访问它,或者如果它 public(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 的文档上运行良好,但我的搜索索引无法 return 那些 public 设置为 true 的文档。我的搜索查询看起来像这样:

q=user_id:1

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

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

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

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