Repository navigation
Not allow to sort by id - #14526
Not allow to sort by id#14526
Conversation
📝 WalkthroughWalkthroughUpdates commented version strings in Docker configuration from v0.25.0 to v0.25.1, and adds logic to both Elasticsearch connection utilities to exclude "id" fields from sort clauses, preventing sort-related errors. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
rag/utils/es_conn.py (1)
238-264:⚠️ Potential issue | 🟠 Major
id-only sorting can incorrectly triggersearch_afterpath, breaking deep pagination.When
order_bycontains only theidfield, theidis skipped at line 247-248 viacontinue, leavingordersempty. However,has_explicit_sortat line 258 still evaluates toTruebecause it checks the raworder_by.fieldsrather than the effectiveorderslist. This causesuse_search_afterto becomeTrue, triggering the_search_with_search_aftermethod, which expects sort values in the response hits (line 102 of that method). Without an actual sort clause sent to Elasticsearch, the hits lack the requiredsortfield, breaking pagination.Additionally, the new
id-skipping flow lacks logging as required by coding guidelines.Suggested fix
+ has_effective_sort = False if order_by: orders = list() for field, order in order_by.fields: order = "asc" if order == 0 else "desc" if field in ["page_num_int", "top_int"]: order_info = {"order": order, "unmapped_type": "float", "mode": "avg", "numeric_type": "double"} elif field.endswith("_int") or field.endswith("_flt"): order_info = {"order": order, "unmapped_type": "float"} elif field == "id": + self.logger.debug("ESConnection.search dropping unsupported sort field 'id'") continue else: order_info = {"order": order, "unmapped_type": "text"} orders.append({field: order_info}) + has_effective_sort = True + if has_effective_sort: + s = s.sort(*orders) - s = s.sort(*orders) - has_explicit_sort = bool(order_by and order_by.fields) + has_explicit_sort = bool(has_effective_sort)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@rag/utils/es_conn.py` around lines 238 - 264, The code incorrectly treats an `id`-only order_by as an explicit sort because has_explicit_sort checks order_by.fields instead of the effective orders sent to Elasticsearch; update the logic so has_explicit_sort is computed from the built orders list (i.e., set has_explicit_sort = bool(orders) or similar) after the loop that builds orders, and ensure you only call s.sort(...) when orders is non-empty; also add a debug/info log when you skip an `id` field (the continue branch) mentioning the field was skipped to satisfy logging guidelines and help debugging; reference symbols: order_by, orders list, s.sort, has_explicit_sort, use_search_after, MatchDenseExpr, and _search_with_search_after.
🧹 Nitpick comments (1)
memory/utils/es_conn.py (1)
209-210: ⚡ Quick winAdd observability when
idsort is skipped.This new branch silently drops a requested sort key; please emit a debug/warn log before
continueso this behavior is diagnosable in production.As per coding guidelines,
**/*.py: Add logging for new flows.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@memory/utils/es_conn.py` around lines 209 - 210, In the branch that checks elif field == "id" (the sort-building code where an "id" sort is currently skipped), add an observability log before the continue—use the module logger (get or create logging.getLogger(__name__) as `logger`) and emit a warning or debug message that clearly states the requested sort key "id" was skipped (include the field value and any surrounding context like the original sort request or index name if available); then proceed with the existing continue so the runtime behavior is unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@rag/utils/es_conn.py`:
- Around line 238-264: The code incorrectly treats an `id`-only order_by as an
explicit sort because has_explicit_sort checks order_by.fields instead of the
effective orders sent to Elasticsearch; update the logic so has_explicit_sort is
computed from the built orders list (i.e., set has_explicit_sort = bool(orders)
or similar) after the loop that builds orders, and ensure you only call
s.sort(...) when orders is non-empty; also add a debug/info log when you skip an
`id` field (the continue branch) mentioning the field was skipped to satisfy
logging guidelines and help debugging; reference symbols: order_by, orders list,
s.sort, has_explicit_sort, use_search_after, MatchDenseExpr, and
_search_with_search_after.
---
Nitpick comments:
In `@memory/utils/es_conn.py`:
- Around line 209-210: In the branch that checks elif field == "id" (the
sort-building code where an "id" sort is currently skipped), add an
observability log before the continue—use the module logger (get or create
logging.getLogger(__name__) as `logger`) and emit a warning or debug message
that clearly states the requested sort key "id" was skipped (include the field
value and any surrounding context like the original sort request or index name
if available); then proceed with the existing continue so the runtime behavior
is unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: be502b9c-3e0e-44ae-b36e-a4d91a9c201a
📒 Files selected for processing (3)
docker/.envmemory/utils/es_conn.pyrag/utils/es_conn.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14526 +/- ##
=======================================
Coverage 94.16% 94.16%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 662 662
Misses 25 25
Partials 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
id as "text", not a "keyword", order by it will cause error.
Type of change