具有多个 .contains() 的 RethinkDB 索引查询

RethinkDB index query with several .contains()

我有以下查询工作正常但速度很慢,但是我不知道如何正确索引它:

r.db('my_db')
.table('messages')
.filter({ community_id : community.id})
.filter(function(row){
    return row('mentions').contains(user.id);
})
.filter(function(row){
    return row('channels').contains(channel.id);
})
.orderBy(r.desc('created_at'))
.skip(0)
.limit(50);

我尝试使用以下索引(使用 Thinky.js):

Model.ensureIndex("user_mentions", function(message){
    return message("mentions").map(function(user_id){
        return message("channels").map(function(channel_id){
            return [ 
                message("community_id"), 
                message("mentions").contains(user_id),
                message("channels").contains(channel_id), 
                message('created_at') 
            ];
        }); 
    });

}, {multi: true});

然后要查询它,我试过这个:

r.db('my_db')
.table('messages')
.between(
    [community.id, user.id, channel.id, r.minval], 
    [community.id, data.user.id, channel.id, r.maxval], 
    { index : 'user_mentions' }
)
.orderBy({index:r.desc("user_mentions")})
.skip(0)
.limit(50);

消息 table 看起来像:

id | community_id | mentions (array of user_ids) | channels (array of channel_ids) | created_at

但我最终得到的结果为零。 我非常感谢任何建议!

我认为该索引将使您在上面编写的 between 查询有效:

.indexCreate(function(message) {
  return message('channels').concatMap(function(channel) {
    return message('mentions').map(function(mention) {
      return [
        message('community_id'),
        mention,
        channel,
        message('created_at')
      ];
    });
  });
}, {multi: true});