Pagination and filetering questions

Some questions to help figure out what you are attempting:

Is the post ID (req.params.id) a field on the comments? Then it could be included in the intersection. From the names, and without actual definitions, it looks like Index('all_comments_by_target_post') is the same as Index('comments_by_post').

The intersection will get all comments with the provided Author AND targetPost AND title AND content. That’s a lot of things to “AND”. Is that what you want, or should these be “OR” operations? Union would be used for that.

If your intention is to “OR” all of the search parameters, but “AND” only with the post, i.e.: (post AND (author OR target OR title OR content)) then you could do the following.

Paginate(
  Intersection(
    Match(Index('comments_by_post'), req.params.id),
    Union(
      Match(Index('all_comments_by_comment_author'), commentAuthor),
      Match(Index('all_comments_by_target_post'), targetPost),
      Match(Index('all_comments_by_title'), title),
      Match(Index('all_comments_by_content'), content)
    )
  )
)

Also, you already referenced another post which recommends NOT using Drop for pagination, but instead using the built in pagination. Is there a reason why you are using Drop rather than before and after cursors? Size is not how many are returned if you use Drop, it’s how many are fetched. Eventually as you get to later pages using Drop you will be fetching many MANY more times the documents than needed and dropping most of them.

Maybe this other post will help?

Using the size and after cursor, you can get later pages like this:

Paginate(
  Intersection(
    Match(Index('comments_by_post'), req.params.id),
    Union(
      Match(Index('all_comments_by_comment_author'), commentAuthor),
      Match(Index('all_comments_by_target_post'), targetPost),
      Match(Index('all_comments_by_title'), title),
      Match(Index('all_comments_by_content'), content)
    )
  ),
  {
    size: 20,
    after: [ q.Ref(q.Collection('Comments'), '123456789') ], // <-- the comment that was at the end of the list last page
  }
)