@alfrenerd Thanks for reposting it here. We request all our users to post their questions here instead of Slack for quicker attention.
I took your Index definitions, populated some data, reproduced the issue and provided workaround solution in the below steps. Please be aware that it is a long post but wanted to provide keystroke level examples.
CreateCollection({ name: "posts"});
CreateIndex(
{
name:
"posts_TERMS_ref_VALUES_created_likes_ref",
source: Collection("posts"),
terms: [{ field: ["ref"] }],
values: [
{ field: ["data", "created"], reverse: true },
{ field: ["data", "likes"], reverse: true },
{ field: ["ref"] },
],
serialized: true,
}
)
CreateIndex({
name: "posts_TERMS_author_VALUES_ref",
source: Collection("posts"),
terms: [
{
field: ["data", "author"],
},
],
values: [{ field: ["ref"] }],
serialized: true,
})
Create some sample documents in the collection. ( All documents with author = âJayâ and created between â2020-11-01â to â2020-11-05â)
Map(
[
{
author: "jay",
created: "2020-11-01",
likes: "100"
},
{
author: "jay",
created: "2020-11-01",
likes: "300"
},
{
author: "jay",
created: "2020-11-02",
likes: "1000"
},
{
author: "jay",
created: "2020-11-03",
likes: "256"
},
{
author: "jay",
created: "2020-11-03",
likes: "108"
},
{
author: "jay",
created: "2020-11-04",
likes: "30000"
},
{
author: "jay",
created: "2020-11-05",
likes: "1"
}
],
Lambda("x", Create(Collection("posts"), {data: Var("x")}))
)
Used the join ( same as the one you posted) to Get all the posts where author="jay" sorted by created desc , likes desc
Paginate(Join(
Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
Lambda(
["postRef"],
Match(
Index(
"posts_TERMS_ref_VALUES_created_likes_ref"
),
Var("postRef")
)
)
))
{
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")],
["2020-11-03", "256", Ref(Collection("posts"), "281937619999265282")],
["2020-11-03", "108", Ref(Collection("posts"), "281937619999263234")],
["2020-11-02", "1000", Ref(Collection("posts"), "281937619999267330")],
["2020-11-01", "300", Ref(Collection("posts"), "281937619999266306")],
["2020-11-01", "100", Ref(Collection("posts"), "281937619999261186")]
]
}
So far all good, let us limit the page size:2 to play around with after
cursor.
Paginate(Join(
Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
Lambda(
["postRef"],
Match(
Index(
"posts_TERMS_ref_VALUES_created_likes_ref"
),
Var("postRef")
)
)
), {size:2})
{
after: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
],
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")]
]
Now let us get the next page with size:2 and we will use the after
cursor from previous result.
Paginate(Join(
Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
Lambda(
["postRef"],
Match(
Index(
"posts_TERMS_ref_VALUES_created_likes_ref"
),
Var("postRef")
)
)
), {size:2, after: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
]})
{
before: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
],
after: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
],
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")]
]
}
Issue - We still get first page data instead of second page.
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")]
]
Workaround - Instead of Join
please use Lambda
and Union
with the same Indexes.
First let us get all the posts.
Paginate(
Let(
{
author_set: Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
page:Select("data", Paginate(Var("author_set"), { size: 100000 })),
leaf_sets: Map(Var("page"),Lambda("ref",Match(Index("posts_TERMS_ref_VALUES_created_likes_ref"),Var("ref"))))
},
Union(Var("leaf_sets"))
)
)
{
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")],
["2020-11-03", "256", Ref(Collection("posts"), "281937619999265282")],
["2020-11-03", "108", Ref(Collection("posts"), "281937619999263234")],
["2020-11-02", "1000", Ref(Collection("posts"), "281937619999267330")],
["2020-11-01", "300", Ref(Collection("posts"), "281937619999266306")],
["2020-11-01", "100", Ref(Collection("posts"), "281937619999261186")]
]
}
Get the First page with size:2
Paginate(
Let(
{
author_set: Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
page:Select("data", Paginate(Var("author_set"), { size: 100000 })),
leaf_sets: Map(Var("page"),Lambda("ref",Match(Index("posts_TERMS_ref_VALUES_created_likes_ref"),Var("ref"))))
},
Union(Var("leaf_sets"))
),
{size:2})
{
after: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
],
data: [
["2020-11-05", "1", Ref(Collection("posts"), "281937619999264258")],
["2020-11-04", "30000", Ref(Collection("posts"), "281937619999262210")]
]
}
Second page with size:2, you will notice that data
contains next 2 documents.
Paginate(
Let(
{
author_set: Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
page:Select("data", Paginate(Var("author_set"), { size: 100000 })),
leaf_sets: Map(Var("page"),Lambda("ref",Match(Index("posts_TERMS_ref_VALUES_created_likes_ref"),Var("ref"))))
},
Union(Var("leaf_sets"))
),
{size:2,
after: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
]})
{
before: [
"2020-11-03",
"256",
Ref(Collection("posts"), "281937619999265282"),
Ref(Collection("posts"), "281937619999265282")
],
after: [
"2020-11-02",
"1000",
Ref(Collection("posts"), "281937619999267330"),
Ref(Collection("posts"), "281937619999267330")
],
data: [
["2020-11-03", "256", Ref(Collection("posts"), "281937619999265282")],
["2020-11-03", "108", Ref(Collection("posts"), "281937619999263234")]
]
}
Third Page ⌠so on and so forth.
Paginate(
Let(
{
author_set: Match(Index("posts_TERMS_author_VALUES_ref"), "jay"),
page:Select("data", Paginate(Var("author_set"), { size: 100000 })),
leaf_sets: Map(Var("page"),Lambda("ref",Match(Index("posts_TERMS_ref_VALUES_created_likes_ref"),Var("ref"))))
},
Union(Var("leaf_sets"))
),
{size:2,
after: [
"2020-11-02",
"1000",
Ref(Collection("posts"), "281937619999267330"),
Ref(Collection("posts"), "281937619999267330")
]})
{
before: [
"2020-11-02",
"1000",
Ref(Collection("posts"), "281937619999267330"),
Ref(Collection("posts"), "281937619999267330")
],
after: [
"2020-11-01",
"100",
Ref(Collection("posts"), "281937619999261186"),
Ref(Collection("posts"), "281937619999261186")
],
data: [
["2020-11-02", "1000", Ref(Collection("posts"), "281937619999267330")],
["2020-11-01", "300", Ref(Collection("posts"), "281937619999266306")]
]
}
Hope this helps.