Skip to content

Fix: handle id as keyword - #14729

Merged
wangq8 merged 4 commits into
infiniflow:mainfrom
Lynn-Inf:fix/es-mapping
May 9, 2026
Merged

wangq8 merged 4 commits into
infiniflow:mainfrom
Lynn-Inf:fix/es-mapping

Conversation

@Lynn-Inf

@Lynn-Inf Lynn-Inf commented May 9, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Update mapping.json to treat id as a keyword.

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 May 9, 2026
@coderabbitai

coderabbitai Bot commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR broadens the kwd dynamic template regex in conf/mapping.json to include standalone id, and updates two ESConnection.search implementations to use unmapped_type: "keyword" (instead of "text") for fallback non-numeric, non-id sort fields.

Changes

Mapping + Search Sort Changes

Layer / File(s) Summary
Mapping Configuration
conf/mapping.json
kwd dynamic template match regex updated to recognize standalone id in addition to uid and suffix-based patterns; mapping settings unchanged.
Search Helpers (memory)
memory/utils/es_conn.py
Fallback non-numeric, non-id sort clauses now set unmapped_type: "keyword" instead of "text".
Search Helpers (rag)
rag/utils/es_conn.py
Fallback non-numeric, non-id sort clauses now set unmapped_type: "keyword" instead of "text".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • infiniflow/ragflow#14356: Modifies ESConnection.search in rag/utils/es_conn.py and adds special id handling — related to sort behavior and id semantics.
  • infiniflow/ragflow#14526: Adjusts sort-building logic and id handling in ESConnection.search across helpers.

Suggested reviewers

  • wangq8

Poem

🐰 I hop through mappings, sniff the code so slick,
Now id is caught where keywords click.
When sorts run wild and fields are shy,
"keyword" steadies the order—oh my! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix: handle id as keyword' clearly and concisely describes the main change: treating the id field as a keyword in Elasticsearch mapping configuration.
Description check ✅ Passed The description includes the required template sections: problem statement (treating id as keyword) and type of change (Bug Fix). While brief, it provides sufficient information about the core change and its classification.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

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

@Lynn-Inf Lynn-Inf added 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration labels May 9, 2026
@Lynn-Inf
Lynn-Inf marked this pull request as draft May 9, 2026 07:36
@Lynn-Inf
Lynn-Inf marked this pull request as ready for review May 9, 2026 07:36
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 9, 2026
@codecov

codecov Bot commented May 9, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14729   +/-   ##
=======================================
  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.

@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.

Actionable comments posted: 1

♻️ Duplicate comments (1)
memory/utils/es_conn.py (1)

209-210: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Update stale comment to reflect that id is now a keyword.

This comment has the same issue as in rag/utils/es_conn.py. The comment states "id as 'text', not a 'keyword'", but the PR objective is to treat id as a keyword. Additionally, verify whether the continue statement is still necessary if id is now properly mapped as a keyword.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@memory/utils/es_conn.py` around lines 209 - 210, The comment and behavior in
the branch that checks elif field == "id" need updating: change the comment to
state that id is now mapped as a keyword (not text) and therefore safe to sort
on, and remove the unconditional continue if sorting/ordering logic expects
keyword fields; update the elif field == "id" branch in the function that builds
ES sort/field lists (the branch using the local variable field) to either delete
the special-case branch entirely or adjust it to allow id through as a keyword,
and run the code-path that handles keyword fields so id is treated the same as
other keyword fields.
🧹 Nitpick comments (1)
memory/utils/es_conn.py (1)

268-268: ⚡ Quick win

Consider updating unmapped_type to "keyword" for consistency.

The get_forgotten_messages and get_missing_field_message methods still use unmapped_type: "text" for non-numeric fields, while the main search method now uses "keyword". For consistent sorting behavior across all methods, consider applying the same change here.

♻️ Proposed fix for consistency

For line 268 in get_forgotten_messages:

             else:
-                order_info = {"order": order, "unmapped_type": "text"}
+                order_info = {"order": order, "unmapped_type": "keyword"}
             orders.append({field: order_info})

For line 313 in get_missing_field_message:

             else:
-                order_info = {"order": order, "unmapped_type": "text"}
+                order_info = {"order": order, "unmapped_type": "keyword"}
             orders.append({field: order_info})

Also applies to: 313-313

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@memory/utils/es_conn.py` at line 268, In get_forgotten_messages and
get_missing_field_message update the unmapped_type used in the order_info dict
from "text" to "keyword" so sorting behavior matches the main search method;
locate the order_info assignment (e.g. order_info = {"order": order,
"unmapped_type": "text"}) in both functions and change the value to "keyword".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rag/utils/es_conn.py`:
- Around line 247-248: The code currently skips sorting on the id field with
`elif field == "id": continue` and the comment claims id is mapped as "text",
but conf/mapping.json now maps id as a keyword via the "kwd" dynamic template;
update the comment to reflect that id is a keyword and remove or conditionally
guard the `continue` so sorting on `id` is allowed. Specifically, in the block
checking `field == "id"` (and any adjacent sort-handling code), change the
comment to mention the "kwd" dynamic template mapping and either delete the
`continue` or replace it with a runtime check of the index mapping (e.g., only
skip if the field is non-keyword) so keyword id fields can be used for ordering.
Ensure references to the `field` variable, the `elif field == "id"` branch, and
conf/mapping.json's "kwd" pattern are preserved to locate the change.

---

Duplicate comments:
In `@memory/utils/es_conn.py`:
- Around line 209-210: The comment and behavior in the branch that checks elif
field == "id" need updating: change the comment to state that id is now mapped
as a keyword (not text) and therefore safe to sort on, and remove the
unconditional continue if sorting/ordering logic expects keyword fields; update
the elif field == "id" branch in the function that builds ES sort/field lists
(the branch using the local variable field) to either delete the special-case
branch entirely or adjust it to allow id through as a keyword, and run the
code-path that handles keyword fields so id is treated the same as other keyword
fields.

---

Nitpick comments:
In `@memory/utils/es_conn.py`:
- Line 268: In get_forgotten_messages and get_missing_field_message update the
unmapped_type used in the order_info dict from "text" to "keyword" so sorting
behavior matches the main search method; locate the order_info assignment (e.g.
order_info = {"order": order, "unmapped_type": "text"}) in both functions and
change the value to "keyword".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 310edc63-db97-4559-89a1-167cc7368686

📥 Commits

Reviewing files that changed from the base of the PR and between 9424f55 and e54f184.

📒 Files selected for processing (2)
  • memory/utils/es_conn.py
  • rag/utils/es_conn.py

Comment thread rag/utils/es_conn.py
Comment on lines 247 to 248
elif field == "id":
continue # id as "text", not a "keyword", order by it will cause error

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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check if id is mapped as keyword in the mapping configuration

# Search for the kwd dynamic template and id field mapping
rg -nC5 '"kwd"' conf/mapping.json
rg -nC3 '"id"' conf/mapping.json

Repository: infiniflow/ragflow

Length of output: 362


Update the stale comment and reconsider the skip logic for id field.

The comment states "id as 'text', not a 'keyword'", but conf/mapping.json now maps id as a keyword field via the "kwd" dynamic template (pattern: ^(.*_(kwd|id|ids|uid|uids)|uid|id)$, type: keyword). Update the comment to reflect this change. Additionally, since id is now a keyword, verify whether the continue statement is still needed—sorting by keyword fields should not cause errors.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rag/utils/es_conn.py` around lines 247 - 248, The code currently skips
sorting on the id field with `elif field == "id": continue` and the comment
claims id is mapped as "text", but conf/mapping.json now maps id as a keyword
via the "kwd" dynamic template; update the comment to reflect that id is a
keyword and remove or conditionally guard the `continue` so sorting on `id` is
allowed. Specifically, in the block checking `field == "id"` (and any adjacent
sort-handling code), change the comment to mention the "kwd" dynamic template
mapping and either delete the `continue` or replace it with a runtime check of
the index mapping (e.g., only skip if the field is non-keyword) so keyword id
fields can be used for ordering. Ensure references to the `field` variable, the
`elif field == "id"` branch, and conf/mapping.json's "kwd" pattern are preserved
to locate the change.

@wangq8
wangq8 merged commit efe6d23 into infiniflow:main May 9, 2026
1 check passed
Ricardo-M-L pushed a commit to Ricardo-M-L/ragflow that referenced this pull request Aug 17, 2026
### What problem does this PR solve?

Update mapping.json to treat id as a keyword.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants