Skip to content

Optimize FindQuery.update() with partial writes - #843

Open
lh0156 wants to merge 8 commits into
redis:mainfrom
lh0156:agent/find-query-update-777
Open

Optimize FindQuery.update() with partial writes#843
lh0156 wants to merge 8 commits into
redis:mainfrom
lh0156:agent/find-query-update-777

Conversation

@lh0156

@lh0156 lh0156 commented Jul 26, 2026

Copy link
Copy Markdown

Summary

Implements #777 by updating matching records without materializing full Pydantic models.

What changed

  • Uses FT.SEARCH ... NOCONTENT to collect matching keys page by page.
  • Validates and serializes update values once, including Pydantic constraints and Redis OM storage conversions.
  • Uses partial HSET mappings for HashModel.
  • Uses path-level JSON.SET commands for JsonModel, including nested __ paths.
  • Preserves the use_transaction flag and returns the number of updated records.
  • Keeps query projections out of the key-only search and preserves KNN state when copying queries.
  • Documents the new return value.

Testing

  • pytest -q tests --ignore tests/test_benchmarks.py: 265 passed.
  • pytest -q tests/test_hash_model.py tests/test_json_model.py: 147 passed.
  • Generated sync tests for the new coverage plus Hash/JSON model tests: 157 passed.
  • ruff check and ruff format --check on changed source/tests.
  • python -m compileall -q aredis_om tests.
  • bandit -q -r aredis_om/model/model.py -s B608.

Redis Stack was run locally via Docker Compose for the integration tests; benchmark tests were excluded from the full run.


Note

Medium Risk
Bulk updates now bypass per-instance save() and write directly to Redis, so behavior depends on the new serialization and TTL preservation matching prior semantics; scope is contained to FindQuery.update() with broad test coverage.

Overview
FindQuery.update() no longer loads full models and re-saves them. It validates and serializes update values once, pages FT.SEARCH … NOCONTENT to collect matching keys (ignoring .only() projections), then applies partial HSET mappings for HashModel or JSON.SET on dotted paths (including __ nested fields) for JsonModel. Hash updates re-apply HEXPIRE for fields that had a positive TTL. The method now returns the count of updated records; docs reflect that.

FindQuery.dict() / copy() also persist knn when cloning queries for the key-only search path.

New unit tests cover validation-before-search, empty hash mappings, pagination, JSON paths, and Pydantic v1 field resolution; an integration test asserts bulk update preserves hash field TTLs.

Reviewed by Cursor Bugbot for commit 4ca575f. Bugbot is set up for automated code reviews on this repo. Configure here.

@lh0156
lh0156 marked this pull request as ready for review July 30, 2026 09:12
Comment thread aredis_om/model/model.py
Comment thread aredis_om/model/model.py Outdated
@abrookins

Copy link
Copy Markdown
Collaborator

Thanks for opening, @lh0156! I'll take a look ASAP.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 256a3c8. Configure here.

Comment thread aredis_om/model/model.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants