Skip to content

Commit 49a4a1e

Browse files
author
Simon Prickett
authored
Merge pull request #219 from redis/fix-unnecessary-commands-for-first-search-#218
Performance improvement for first - avoids unnecessary pagination.
2 parents c0fa1be + e5c6c04 commit 49a4a1e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

aredis_om/model/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ async def execute(self, exhaust_results=True):
749749

750750
async def first(self):
751751
query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields)
752-
results = await query.execute()
752+
results = await query.execute(exhaust_results=False)
753753
if not results:
754754
raise NotFoundError()
755755
return results[0]

tests/test_hash_model.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Member(BaseHashModel):
4646
last_name: str = Field(index=True)
4747
email: str = Field(index=True)
4848
join_date: datetime.date
49-
age: int = Field(index=True)
49+
age: int = Field(index=True, sortable=True)
5050
bio: str = Field(index=True, full_text_search=True)
5151

5252
class Meta:
@@ -357,6 +357,43 @@ def test_validation_passes(m):
357357
)
358358
assert member.first_name == "Andrew"
359359

360+
@pytest.mark.asyncio
361+
async def test_retrieve_first(m):
362+
member = m.Member(
363+
first_name="Simon",
364+
last_name="Prickett",
365+
366+
join_date=today,
367+
age=99,
368+
bio="This is the bio field for this user.",
369+
)
370+
371+
await member.save()
372+
373+
member2 = m.Member(
374+
first_name="Another",
375+
last_name="Member",
376+
377+
join_date=today,
378+
age=98,
379+
bio="This is the bio field for this user.",
380+
)
381+
382+
await member2.save()
383+
384+
member3 = m.Member(
385+
first_name="Third",
386+
last_name="Member",
387+
388+
join_date=today,
389+
age=97,
390+
bio="This is the bio field for this user.",
391+
)
392+
393+
await member3.save()
394+
395+
first_one = await m.Member.find().sort_by("age").first()
396+
assert first_one == member3
360397

361398
@pytest.mark.asyncio
362399
async def test_saves_model_and_creates_pk(m):

0 commit comments

Comments
 (0)