Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/core/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,19 @@ impl Database {
.filter_map(Result::ok)
.collect();

// Sort by similarity (highest first)
// Sort by similarity (highest first), handling NaNs
results.sort_by(|a, b| {
b.similarity
.partial_cmp(&a.similarity)
.unwrap_or(std::cmp::Ordering::Equal)
if b.similarity.is_nan() && a.similarity.is_nan() {
std::cmp::Ordering::Equal
} else if b.similarity.is_nan() {
std::cmp::Ordering::Less
} else if a.similarity.is_nan() {
std::cmp::Ordering::Greater
} else {
b.similarity
.partial_cmp(&a.similarity)
.unwrap_or(std::cmp::Ordering::Equal)
}
});

results.truncate(limit * 3); // Get more for reranking
Expand Down
16 changes: 12 additions & 4 deletions src/core/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ impl SearchEngine {
})
.collect();

// Sort by score descending
// Sort by score descending, handling NaNs by pushing them to the end
results.sort_by(|a, b| {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
if b.score.is_nan() && a.score.is_nan() {
std::cmp::Ordering::Equal
} else if b.score.is_nan() {
std::cmp::Ordering::Less
} else if a.score.is_nan() {
std::cmp::Ordering::Greater
} else {
b.score
.partial_cmp(&a.score)
.unwrap_or(std::cmp::Ordering::Equal)
}
});

results.truncate(max_results);
Expand Down
Loading