【问题标题】:Cloudant search index selective queryCloudant 搜索索引选择性查询
【发布时间】:2016-08-26 09:20:18
【问题描述】:

您好,我有以下用例,假设我有一个包含以下字段的 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 的文档,但我的搜索索引无法返回那些 public 设置为 true 的文档。我的搜索查询如下所示:

q=user_id:1

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

【问题讨论】:

    标签: couchdb cloudant nosql


    【解决方案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。

    【讨论】:

    • 感谢您的建议,还有一个后续问题是,如果我要为此目的使用视图,如何集成按时间戳排序的功能?
    • 在这个键中,你可以发出一个键数组。例如:emit(['',(new Date()).getTime()]);然后,要控制排序,您需要使用 *descending 查询参数。通常,如果我错了,请纠正我,它会对第一个键和第二个键进行排序。最后,它将首先对主键(文档 ID)进行排序。然后对于所有具有相同键的文档,它应该按第二个键(时间戳)排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多