您当前的查询使用“all_school_queries”索引,我假设它只返回引用。这意味着它将在值中具有ref或在值中根本没有值(因为 ref 将是默认返回值)。因此,目前您的索引可能如下所示:
CreateIndex( { name: "all_school_queries", source: Collection('') } ) 要对这个索引进行排序,我们需要添加值(目前它是按引用排序)。所以为了得到一个(几乎)等价的索引,我们可以这样写。
CreateIndex({
name: "all_school_queries",
source: Collection('<yourcollection>'),
values: values: [
{
field: ["ref"]
}
]
})
价值观决定:
- 索引将返回什么
- 该索引将以什么顺序返回这些值(再次通过 ref here)
- 我们可以在这个索引上写什么范围查询,在上面的例子中我们可以在 ref 上写一个范围查询(这当然没用)。
然而,我们可以添加任意数量的值,所以让我们确保我们可以按 ts 排序
CreateIndex({
name: "all_school_queries",
source: Collection('<yourcollection>'),
values: values: [
{
field: ["ts"]
},
{
field: ["ref"]
}
]
})
这给你买了两件事。现在您的索引将按 ts(然后按 ref)排序,您也可以在时间戳上编写范围查询。它还会更改您的索引返回的内容,它现在将返回一个 ts 和一个 ref。您现在可以选择添加您想要的所有数据(如果您的数据发生更改,灵活性会降低)或将 ref 作为额外参数返回并使用 Map/Get 来获取实际对象。您只需稍微重写您的查询:
await client.query(
q.Map(
q.Paginate(
q.Match(q.Index("all_school_queries_by_ts")),
{size:1000}
),
q.Lambda(
["ts", "ref"], // we now have two parameters
q.Get(q.Var("ref")) // and only use the ref, but the result will be automatically sorted by ts
)
)
)
仅供参考,如果您更喜欢使用 UI 创建索引,它的外观如下:
