Rethinkdb 为过滤器重新定义DB索引+;排序子句

Rethinkdb 为过滤器重新定义DB索引+;排序子句,rethinkdb,Rethinkdb,假设注释表具有以下结构: id | author | timestamp | body 我想使用索引高效地执行以下查询: r.table('comments').getAll("me", {index: "author"}).orderBy('timestamp').run(conn, callback) 我还可以使用其他有效的方法吗? 表的筛选结果似乎不支持当前索引。为timestamp创建索引并将其作为提示添加到orderBy('timestamp',{index:timestamp})

假设注释表具有以下结构:

id | author | timestamp | body
我想使用索引高效地执行以下查询:

r.table('comments').getAll("me", {index: "author"}).orderBy('timestamp').run(conn, callback)
我还可以使用其他有效的方法吗?

表的筛选结果似乎不支持当前索引。为
timestamp
创建索引并将其作为提示添加到
orderBy('timestamp',{index:timestamp})
中时,我遇到以下错误:

RqlRuntimeError:只能对表执行索引顺序_by。在:


目前不可能使用索引两次将
getAll
orderBy
链接起来。 现在只能在表上使用索引进行排序


注意:带索引的orderBy命令是
orderBy({index:'timestamp'})
(无需重复键)

这可以通过在“author”和“timestamp”字段上的复合索引来实现。您可以创建这样的索引,如下所示:

r.table("comments").index_create("author_timestamp", lambda x: [x["author"], x["timestamp"]])
然后您可以使用它执行如下查询:

r.table("comments")
 .between(["me", r.minval], ["me", r.maxval]
 .order_by(index="author_timestamp)

像get_这样的中间工作都是在原始查询中完成的,因为它只获取具有作者“我”和任何时间戳的文档。然后我们对按时间戳排序的同一索引进行排序(因为所有键都有相同的作者)。这里的关键是,每个表访问只能使用一个索引,因此我们需要将所有这些信息塞进同一个索引。

选择了Joe Doliner的答案,但我认为这是错误的

首先,在
between
命令中,没有指定索引器。因此,之间的
将使用主索引

其次,中间的
返回一个选择

table.between(lowerKey, upperKey[, {index: 'id', leftBound: 'closed', rightBound: 'open'}]) → selection
并且,
orderBy
不能在带有索引的选择上运行,只有表可以使用索引

table.orderBy([key1...], {index: index_name}) → selection<stream>
selection.orderBy(key1, [key2...]) → selection<array>
sequence.orderBy(key1, [key2...]) → array
table.orderBy([key1…],{index:index_name})→ 选择
selection.orderBy(键1、[键2…]))→ 选择
sequence.orderBy(key1,[key2…]))→ 排列

您想创建一个所谓的“复合索引”。之后,您可以高效地查询它

//create compound index r.table('comments') .indexCreate( 'author__timestamp', [r.row("author"), r.row("timestamp")] ) //the query r.table('comments') .between( ['me', r.minval], ['me', r.maxval], {index: 'author__timestamp'} ) .orderBy({index: r.desc('author__timestamp')}) //or "r.asc" .skip(0) //pagi .limit(10) //nation! //创建复合索引 r、 表('注释') .取消创建( '作者时间戳',[r.row(“作者”),r.row(“时间戳”)] ) //询问 r、 表('注释') .中间( ['me',r.minval], ['me',r.maxval], {索引:“作者时间戳”} ) .orderBy({index:r.desc('author\u timestamp')})//或“r.asc” .skip(0)//pagi .限制(10)//国家! 我喜欢在复合索引中使用两个下划线。只是风格而已。选择如何命名复合索引并不重要


参考资料:

是否可以对多个作者执行此操作?i、 e.您希望检索作者
John
Jane
最近的10条评论。