0

I need to implement cursor based pagination on an endpoint which accepts a search query. The problem is that since rows will be ranked based on a relevance score derived from a full-text search in natural language, IDs won't be sequential. Does anyone know how I can achieve this in EF Core with Pomelo.EntityFrameworkCore.MySql?

1
  • See if Shorpy could help. Commented Mar 19 at 15:53

1 Answer 1

0

You have the "relevance score", correct? And a sequential id?

"Remember where you left off" and use the pair (score, id) to keep track of the last item on one page (or equivalently, the first item on the next page).

More discussion: https://mysql.rjweb.org/doc.php/pagination

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for the late reply. I do have sequential IDs, but the problem is they won't be sequential since the relevance score is more important. Consider the case where each page contains 2 rows and that there are 4 rows: ID = 1, Score = 2 ID = 2, Score = 3 ID = 3, Score = 4 ID = 4, Score = 3 Given I want to return from most to least relevant, the first request will yield [(ID = 3, Score = 4), (4, 3)], the second request will look for rows with ID > 4 (last) AND Score <= 3; with this query rows 1 and 2 won't be returned because their ID is lesser than 4.
@iKingNinja - For effectively ORDER BY score DESC, id ASC: [4,3], [3,2], [3,4], [2,1] or for ORDER BY score DESC, id DESC: [4,3], [3,4], [3,2], [2,1] (Where the pairs are [score,id]
I don't get what you're saying. My question is how will I handle pages where the first item has the same relevance score as the last item in the previous one since score based sorting makes IDs non-sequential?
@iKingNinja - The combination of score and id is unique for each page and provides a predictable ordering to the pages.
So is there a one-fits-all query? Could you make an example with some dummy data where ordering by ID won't exclude any row with the relevant SQL query? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.