filter + orderby的RethinkDB索引

Rob*_*mba 7 rethinkdb

让我们说评论表具有以下结构:

id | author | timestamp | body
Run Code Online (Sandbox Code Playgroud)

我想使用索引来有效地执行以下查询:

r.table('comments').getAll("me", {index: "author"}).orderBy('timestamp').run(conn, callback)
Run Code Online (Sandbox Code Playgroud)

我可以使用其他有效的方法吗?

看起来当前索引不支持表的过滤结果.在创建索引timestamp并将其添加为提示时,orderBy('timestamp', {index: timestamp})我收到以下错误:

RqlRuntimeError:索引order_by只能在TABLE上执行.在:

Joe*_*ner 14

这可以通过"author"和"timestamp"字段上的复合索引来完成.您可以像这样创建这样的索引:

r.table("comments").index_create("author_timestamp", lambda x: [x["author"], x["timestamp"]])
Run Code Online (Sandbox Code Playgroud)

然后您可以使用它来执行查询,如下所示:

r.table("comments")
 .between(["me", r.minval], ["me", r.maxval]
 .order_by(index="author_timestamp)
Run Code Online (Sandbox Code Playgroud)

两者之间的作用类似于原始查询中的get_all,因为它只获取具有作者"me"和任何时间戳的文档.然后我们对按时间戳排序的相同索引执行order_by(因为所有键具有相同的作者.)这里的关键是每个表访问只能使用一个索引,所以我们需要将所有这些信息填入相同的指数.