Repository navigation
Fix: allow search id or _id - #14356
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
rag/utils/es_conn.py (1)
173-182: Consider usingQ("ids", values=...)for the_idside for consistency withdelete()and idiomatic ES.Two observations on the new
idbranch:
Consistency with
delete(): Thedelete()method in this same file (lines 530–536) usesQ("ids", values=chunk_ids)to match the_idmetadata field. The new search branch usesterm/termsdirectly on_idinstead. While term/terms queries on_idare fully supported across all Elasticsearch versions, aligning both code paths to useidsis more idiomatic and reduces the chance of subtle divergence if either method is later modified.Error handling consistency: Unlike the generic branch below (lines 189–191), this new branch silently
continues for anyvthat is notlist/str/int(e.g.,dict). Consider falling through to the shared type check or explicitly raising the same exception so callers get consistent error feedback for malformed conditions.♻️ Suggested refactor
if k == "id": if not v: continue if isinstance(v, list): bool_query.filter.append( - Q("bool", should=[Q("terms", id=v), Q("terms", _id=v)], minimum_should_match=1)) + Q("bool", + should=[Q("terms", id=v), Q("ids", values=v)], + minimum_should_match=1)) elif isinstance(v, str) or isinstance(v, int): bool_query.filter.append( - Q("bool", should=[Q("term", id=v), Q("term", _id=v)], minimum_should_match=1)) + Q("bool", + should=[Q("term", id=v), Q("ids", values=[v])], + minimum_should_match=1)) + else: + raise Exception( + f"Condition `id={v!s}` value type is {type(v)!s}, expected to be int, str or list.") continue🤖 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 173 - 182, The id-handling branch in the search builder should use the Elasticsearch ids query for the document id side and add consistent type/error handling: replace the Q("term"/"terms", _id=...) uses with Q("ids", values=...) when v is a single id or list (matching how delete() uses Q("ids", values=chunk_ids")), and for unsupported types (e.g., dict) do not silently continue — either fall through to the shared type-check logic or raise the same TypeError used by the generic branch so callers receive consistent feedback; update the bool_query.filter append sites where Q(...) is constructed and keep the existing handling for the non-_id field (id vs _id) intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@rag/utils/es_conn.py`:
- Around line 173-182: The id-handling branch in the search builder should use
the Elasticsearch ids query for the document id side and add consistent
type/error handling: replace the Q("term"/"terms", _id=...) uses with Q("ids",
values=...) when v is a single id or list (matching how delete() uses Q("ids",
values=chunk_ids")), and for unsupported types (e.g., dict) do not silently
continue — either fall through to the shared type-check logic or raise the same
TypeError used by the generic branch so callers receive consistent feedback;
update the bool_query.filter append sites where Q(...) is constructed and keep
the existing handling for the non-_id field (id vs _id) intact.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9b9087d0-deb6-4111-b082-b391a055eda4
📒 Files selected for processing (1)
rag/utils/es_conn.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14356 +/- ##
=======================================
Coverage 95.30% 95.30%
=======================================
Files 10 10
Lines 703 703
Branches 112 112
=======================================
Hits 670 670
Misses 16 16
Partials 17 17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
### What problem does this PR solve? Allow search id or _id when using es as doc_engine. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
|
I got the following exception in the just-released 0.25.1:
I made a walk-around by skip 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"}
# walk-around: skip `id` in sort list due to #14307
elif field == "id":
continue
else:
order_info = {"order": order, "unmapped_type": "text"}
orders.append({field: order_info})
s = s.sort(*orders) |
@mustangxu let me check |
|
sort by id does not make any sense because id is mapped as a text field, which is not for sorting, sort requires a keyword field, we should not sort by id. it is not an issue of this PR, but to limit the sort by to not support id. |
|
@mustangxu raised a PR to not allow sort by it - #14526 |
What problem does this PR solve?
Allow search id or _id when using es as doc_engine.
Type of change