redis-om-python icon indicating copy to clipboard operation
redis-om-python copied to clipboard

Fix: Pagination to consider limit value while .all() will keep returning all records

Open iamvishalkhare opened this issue 3 years ago • 2 comments

Pagination fix

Problem? Often times in production we don't want to return all matching records instead returning a paginated response based on values of limit and offset is desirable.

What is being fixed? Calling .all() will keep returning all records matching the search criterion whereas if you set value of limit and then call .execute() with exhaust_results=False parameter, It will return number of records equal to value of limit.

Usage- Let us consider 3 scenarios-

Scenario 1

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.limit = 5
model.offset = 0
model.execute(exhaust_results=False)

This will return first 5 records matching search criterion.

Scenario 2

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.execute()

This will return all records matching search criterion.

Scenario 3

model = Model.find(
    (condition 1) &
    (condition 2) &
    ....
    (condition n)
)
model.execute(exhaust_results=False)

This will return top 10 records matching search criterion because default value of limit is 10

How is it fixed? To break away from loop executing the query a condition is added wherein if total number of fetched records is equal or greater than limit. (see file changes)

iamvishalkhare avatar Jun 25 '22 16:06 iamvishalkhare

@simonprickett - Any input is appreciated

iamvishalkhare avatar Jun 25 '22 16:06 iamvishalkhare

@dvora-h can you please have a look?

chayim avatar Jul 05 '22 14:07 chayim