Merge all shard results and return the global top-K. Sorts by distance ascending (nearest first) and truncates to `top_k`. Ties are broken by shard_id for deterministic ordering.
(&mut self, top_k: usize)
| 75 | /// Sorts by distance ascending (nearest first) and truncates to `top_k`. |
| 76 | /// Ties are broken by shard_id for deterministic ordering. |
| 77 | pub fn top_k(&mut self, top_k: usize) -> Vec<VectorHit> { |
| 78 | self.all_hits.sort_by(|a, b| { |
| 79 | a.distance |
| 80 | .partial_cmp(&b.distance) |
| 81 | .unwrap_or(std::cmp::Ordering::Equal) |
| 82 | .then(a.shard_id.cmp(&b.shard_id)) |
| 83 | }); |
| 84 | self.all_hits.truncate(top_k); |
| 85 | self.all_hits.clone() |
| 86 | } |
| 87 | |
| 88 | /// Number of total hits collected (before merge). |
| 89 | pub fn total_hits(&self) -> usize { |