Disclaimer: I’m not that familiar with the python syntax.
So the essence of your query is:
q.let({
"result": q.filter_(
q.and_(
field filters q.equals(
q.select(["data", "filter_field"], q.get(q.var("doc"))),
q.var("filter_field")
)
)
),
q.if_(
q.equals(q.var("index_collection2"), None),
q.match(q.index("index_collection2"))
)
),
"page": q.if_(
q.equals(q.var("before"), None),
q.if_(
q.equals(q.var("after"), None),
q.paginate(set = q.var("result"), size = q.var("size")),
q.paginate(set = q.var("result"), size = q.var("size"), after = q.var("after"))
),
q.paginate(set = q.var("result"), size = q.var("size"), before = q.var("before"))
)
}, #Show results by acquiring every document in the paginated result q.map_(
q.lambda_("docref",
q.get(q.var("docref"))
),
q.var("page")
))
I’m still confused by the question.
Doesn’t it solve your problem to just use the index as you defined it:
CreateIndex(
name: “latest_objects_by_date”,
source: Collection(“Objects”),
values: [
{ field: [“ts”] }, // Somehow hide this in return but also sort them
{ field: [“ref”] }
]
)
Along with a slight modification to your query (namely adding two parameters in the lambda part at the end and only using the ref parameter)
...
}, #Show results by acquiring every document in the paginated result q.map_(
lambda ts, ref: q.get(q.var(ref)),
q.var("page")
))
(not 100% sure if I got that right, as mentioned, never coded with the python driver)