Skip to content

Not allow to sort by id - #14526

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
wangq8:bugfix/not-allow-sort-by-id
Apr 30, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
wangq8:bugfix/not-allow-sort-by-id

Conversation

@wangq8

@wangq8 wangq8 commented Apr 30, 2026 •

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

id as "text", not a "keyword", order by it will cause error.

Type of change

  • Bug Fix (non-breaking change which fixes an issue)

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 30, 2026
@coderabbitai

coderabbitai Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Updates 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

Cohort / File(s) Summary
Docker Configuration
docker/.env
Updated fallback RAGFLOW_IMAGE version strings from v0.25.0 to v0.25.1 in commented registry URL entries (Huawei Cloud and Aliyun).
Elasticsearch Sorting Fix
memory/utils/es_conn.py, rag/utils/es_conn.py
Modified search method's sort-field construction to explicitly exclude "id" fields from Elasticsearch sort clauses, preventing errors when sorting non-keyword text fields.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

size:L

Poem

🐰 A version bump hops through the config land,
While Elasticsearch sorts by a gentler hand,
No more "id" tangles in the sorting spree,
Just cleaner queries, swift and free! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: preventing sorting by the 'id' field to avoid Elasticsearch errors.
Description check ✅ Passed The description includes the required sections with the problem statement (id mapped as text causes errors) and type of change (Bug Fix) clearly identified.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@wangq8
wangq8 marked this pull request as draft April 30, 2026 06:08
@wangq8 wangq8 added the ci Continue Integration label Apr 30, 2026
@wangq8
wangq8 marked this pull request as ready for review April 30, 2026 06:09
@wangq8 wangq8 mentioned this pull request Apr 30, 2026
1 task done

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 trigger search_after path, breaking deep pagination.

When order_by contains only the id field, the id is skipped at line 247-248 via continue, leaving orders empty. However, has_explicit_sort at line 258 still evaluates to True because it checks the raw order_by.fields rather than the effective orders list. This causes use_search_after to become True, triggering the _search_with_search_after method, 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 required sort field, 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 win

Add observability when id sort is skipped.

This new branch silently drops a requested sort key; please emit a debug/warn log before continue so 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

📥 Commits

Reviewing files that changed from the base of the PR and between 4ee0702 and e8ef638.

📒 Files selected for processing (3)
  • docker/.env
  • memory/utils/es_conn.py
  • rag/utils/es_conn.py

@codecov

codecov Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.16%. Comparing base (4ee0702) to head (e8ef638).
⚠️ Report is 5 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8
wangq8 merged commit f45ce00 into infiniflow:main Apr 30, 2026
1 check passed
@mustangxu mustangxu mentioned this pull request May 7, 2026
4 tasks done
@coderabbitai coderabbitai Bot mentioned this pull request May 9, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant