Skip to content

Commit dd13891

Browse files
fix(core): Use AND logic for multi-term package search
Changed the search logic from OR (`.any`) to AND (`.all`) when matching multiple search terms. This significantly improves search precision, as queries like "text editor" will now only return packages matching both terms instead of returning every text-related OR editor-related package. Co-authored-by: insign <1113045+insign@users.noreply.github.com>
1 parent 751ec88 commit dd13891

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

crates/xpm-core/src/db/operations.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Database {
115115
.all()?
116116
.filter_map(|p| p.ok())
117117
.filter(|pkg: &Package| {
118-
terms_lower.iter().any(|term| {
118+
terms_lower.iter().all(|term| {
119119
pkg.name.to_lowercase().contains(term)
120120
|| pkg
121121
.desc
@@ -466,6 +466,29 @@ mod tests {
466466
assert_eq!(results[0].name, "avim");
467467
assert_eq!(results[1].name, "emacs");
468468

469+
// Test AND logic for multi-term search
470+
let p5 = Package {
471+
name: "testpkg".to_string(),
472+
desc: Some("This is a text processing tool".to_string()),
473+
..Package::new("testpkg")
474+
};
475+
db.upsert_package(p5)?;
476+
477+
let p6 = Package {
478+
name: "editorpkg".to_string(),
479+
desc: Some("This is an image editor".to_string()),
480+
..Package::new("editorpkg")
481+
};
482+
db.upsert_package(p6)?;
483+
484+
// Search for "text editor" should NOT return testpkg or editorpkg because they don't match BOTH terms
485+
// It should only return neovim, emacs, and avim (which match both "text" and "editor")
486+
let results = db.search_packages(&["text".to_string(), "editor".to_string()], 10)?;
487+
assert_eq!(results.len(), 3);
488+
assert!(results.iter().any(|p| p.name == "neovim"));
489+
assert!(results.iter().any(|p| p.name == "emacs"));
490+
assert!(results.iter().any(|p| p.name == "avim"));
491+
469492
Ok(())
470493
}
471494
}

0 commit comments

Comments
 (0)