Repository navigation
fix(index): restrict find_related to the seed chunk's language - #123
Conversation
Mirror upstream SembleIndex.find_related: when the seed chunk has a language, pass a same-language selector to the semantic query so related results no longer mix languages. Seeds without a language still search the whole index.
There was a problem hiding this comment.
Code Review
This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when available, and introduces unit tests to validate this logic. Feedback on the changes highlights that user-specified query options, such as path and language filters, are currently ignored when building the selector. It is recommended to merge the seed's language constraint with the user-provided options to ensure all filters are respected.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 4 |
| Duplication | 6 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
cubic analysis
All reported issues were addressed across 2 files
Linked issue analysis
Linked issue: pleaseai/code-search#113: find_related is not restricted to the seed chunk's language (upstream parity)
| Status | Acceptance criteria | Notes |
|---|---|---|
| ✅ | A language-specific seed returns only chunks in that language | find_related builds a language selector, and the mixed-language test confirms only the Rust result is returned. |
| ✅ | A seed without a language searches the whole index | A seed without a language uses no selector, and the test confirms results from both languages. |
Architecture diagram
sequenceDiagram
participant Caller as Caller
participant Csp as CspIndex
participant Selector as Selector Builder
participant Model as Embedding Model
participant SI as Semantic Index
Note over Caller,SI: CspIndex.find_related flow
Caller->>Csp: find_related(seed, options)
Csp->>Csp: Check top_k and chunk count
alt top_k == 0 or no chunks
Csp-->>Caller: Empty Vec
else
alt seed has language
Csp->>Selector: build_selector(filter_languages=[seed.language])
Selector-->>Csp: Same-language selector
else seed has no language
Note over Csp: No selector (whole-index search)
end
Csp->>Model: encode(seed.content)
Model-->>Csp: Query embedding
Csp->>SI: query(embedding, top_k+1, selector)
SI-->>Csp: Candidate batch
alt no candidates
Csp-->>Caller: Empty Vec
else
Note over Csp: Exclude seed, truncate to top_k
Csp-->>Caller: Vec of SearchResult
end
end
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when one is present, and adds corresponding unit tests to verify this behavior. The feedback suggests a performance optimization to return early when the language selector is empty, thereby avoiding expensive model encoding and index querying.
|
/gemini review |
|
There was a problem hiding this comment.
Code Review
This pull request updates the find_related function in CspIndex to restrict search candidates to the seed's language when one is present, and adds corresponding unit tests. Feedback highlights that the implementation currently ignores other query filters (such as path filters) provided in the input options when constructing the candidate selector, and suggests combining the seed's language restriction with the caller's options.
| let selector = seed | ||
| .language | ||
| .as_ref() | ||
| .filter(|lang| !lang.is_empty()) | ||
| .and_then(|lang| { | ||
| self.build_selector(&QueryOptions { | ||
| filter_languages: Some(vec![lang.clone()]), | ||
| ..Default::default() | ||
| }) | ||
| }); |
There was a problem hiding this comment.
[MEDIUM] Query filters in options are ignored in find_related
Problem: The find_related function accepts options: &QueryOptions, but it completely ignores any path filters (filter_paths) or language filters (filter_languages) specified in options when constructing the candidate selector.
Rationale: To ensure correctness and consistency, find_related should respect the query filters provided by the caller, while still overlaying the seed's language restriction when applicable.
Suggestion: Combine the seed's language restriction with the filters from options before building the selector.
| let selector = seed | |
| .language | |
| .as_ref() | |
| .filter(|lang| !lang.is_empty()) | |
| .and_then(|lang| { | |
| self.build_selector(&QueryOptions { | |
| filter_languages: Some(vec![lang.clone()]), | |
| ..Default::default() | |
| }) | |
| }); | |
| let mut query_opts = QueryOptions { | |
| filter_paths: options.filter_paths.clone(), | |
| ..Default::default() | |
| }; | |
| if let Some(lang) = seed.language.as_ref().filter(|lang| !lang.is_empty()) { | |
| query_opts.filter_languages = Some(vec![lang.clone()]); | |
| } else { | |
| query_opts.filter_languages = options.filter_languages.clone(); | |
| } | |
| let selector = self.build_selector(&query_opts); |



Summary
CspIndex::find_relatedqueried the whole semantic index, so related-code results mixed languages (for example a Rust seed returned Python chunks). Upstream semble restricts the query to the seed chunk's language. This PR mirrors that: when the seed has a language,find_relatedbuilds a same-language selector (reusingbuild_selectorwithfilter_languages) and passes it tosemantic_index.query. A seed without a language still searches the whole index. Exclude-seed andtop_ktruncation are unchanged. A selector smaller thantop_k + 1is safe: the backend clampseffective_kto the selector length..please/docs/references/semble.mdsection 4.9 already described find_related as same-language, so no doc change was needed.E2E (temp dir with
a.rs,b.py,c.rs,d.py, find-related ona.rs:1): before the fix the results werec.rsandb.py; after, onlyc.rs.Related issue
Closes #113
Checklist
cargo test --workspace)cargo fmt --allandcargo clippy --all-targets --all-features -- -D warnings)BREAKING CHANGE:note is includedSummary by cubic
Restricts
CspIndex::find_relatedto the seed chunk's language so related-code results no longer mix languages.SembleIndex.find_related.top_ktruncation are unchanged.find_relatedskips embedding and returns no results.Closes #113.
Written for commit 5900043. Summary will update on new commits.