Merge all results: deduplicate by doc_id, optionally sort by distance. For ST_DWithin, results are sorted by distance (nearest first). For boolean predicates, order is arbitrary. Truncates to `limit`.
(&mut self, limit: usize, sort_by_distance: bool)
| 66 | /// For ST_DWithin, results are sorted by distance (nearest first). |
| 67 | /// For boolean predicates, order is arbitrary. Truncates to `limit`. |
| 68 | pub fn merge(&mut self, limit: usize, sort_by_distance: bool) -> Vec<SpatialHit> { |
| 69 | // Deduplicate by doc_id (a document can only appear on one shard, |
| 70 | // but defensive in case of ghost stubs or migration overlap). |
| 71 | let mut seen = std::collections::HashSet::new(); |
| 72 | self.all_hits.retain(|h| seen.insert(h.doc_id.clone())); |
| 73 | |
| 74 | if sort_by_distance { |
| 75 | self.all_hits.sort_by(|a, b| { |
| 76 | a.distance_meters |
| 77 | .partial_cmp(&b.distance_meters) |
| 78 | .unwrap_or(std::cmp::Ordering::Equal) |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | self.all_hits.truncate(limit); |
| 83 | self.all_hits.clone() |
| 84 | } |
| 85 | |
| 86 | /// Total hits collected (before merge). |
| 87 | pub fn total_hits(&self) -> usize { |