| 8 | use std::collections::HashMap; |
| 9 | |
| 10 | pub(crate) fn rank_kg_hits( |
| 11 | txn: &impl crate::pristine::KgTxnT, |
| 12 | hits: HashMap<String, usize>, |
| 13 | limit: usize, |
| 14 | ) -> Result<Vec<KgNode>, crate::pristine::PristineError> { |
| 15 | let mut ranked = Vec::new(); |
| 16 | for (id, hits) in hits { |
| 17 | if let Some(node) = txn.get_kg_node(&id)? { |
| 18 | let score = hits |
| 19 | * if node.has_type(crate::kg::ontology::Class::File) { |
| 20 | 2 |
| 21 | } else { |
| 22 | 1 |
| 23 | }; |
| 24 | ranked.push((score, node)); |
| 25 | } |
| 26 | } |
| 27 | ranked.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.id.cmp(&b.1.id))); |
| 28 | Ok(ranked |
| 29 | .into_iter() |
| 30 | .take(limit) |
| 31 | .map(|(_, node)| node) |
| 32 | .collect()) |
| 33 | } |
| 34 | |
| 35 | /// Type of vault entry. |
| 36 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] |