Skip to content

Commit

Permalink
Merge pull request #219 from redis/fix-unnecessary-commands-for-first…
Browse files Browse the repository at this point in the history
…-search-#218

Performance improvement for first - avoids unnecessary pagination.
  • Loading branch information
Simon Prickett authored Apr 25, 2022
2 parents c0fa1be + e5c6c04 commit 49a4a1e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ async def execute(self, exhaust_results=True):

async def first(self):
query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields)
results = await query.execute()
results = await query.execute(exhaust_results=False)
if not results:
raise NotFoundError()
return results[0]
Expand Down
39 changes: 38 additions & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Member(BaseHashModel):
last_name: str = Field(index=True)
email: str = Field(index=True)
join_date: datetime.date
age: int = Field(index=True)
age: int = Field(index=True, sortable=True)
bio: str = Field(index=True, full_text_search=True)

class Meta:
Expand Down Expand Up @@ -357,6 +357,43 @@ def test_validation_passes(m):
)
assert member.first_name == "Andrew"

@pytest.mark.asyncio
async def test_retrieve_first(m):
member = m.Member(
first_name="Simon",
last_name="Prickett",
email="[email protected]",
join_date=today,
age=99,
bio="This is the bio field for this user.",
)

await member.save()

member2 = m.Member(
first_name="Another",
last_name="Member",
email="[email protected]",
join_date=today,
age=98,
bio="This is the bio field for this user.",
)

await member2.save()

member3 = m.Member(
first_name="Third",
last_name="Member",
email="[email protected]",
join_date=today,
age=97,
bio="This is the bio field for this user.",
)

await member3.save()

first_one = await m.Member.find().sort_by("age").first()
assert first_one == member3

@pytest.mark.asyncio
async def test_saves_model_and_creates_pk(m):
Expand Down

0 comments on commit 49a4a1e

Please sign in to comment.