MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / kg_fts_search

Method kg_fts_search

atomic-core/src/pristine/txn/read.rs:1011–1069  ·  view source on GitHub ↗
(&self, query: &str, limit: usize)

Source from the content-addressed store, hash-verified

1009 }
1010
1011 fn kg_fts_search(&self, query: &str, limit: usize) -> PristineResult<Vec<KgNode>> {
1012 let fts_table = match self.txn.open_multimap_table(KG_FTS) {
1013 Ok(t) => t,
1014 Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()),
1015 Err(e) => return Err(PristineError::from(e)),
1016 };
1017
1018 let tokens = tokenize_for_fts(query);
1019 if tokens.is_empty() {
1020 return Ok(Vec::new());
1021 }
1022
1023 // Collect node IDs that match any token, count matches per node
1024 let mut hit_counts: std::collections::HashMap<String, usize> =
1025 std::collections::HashMap::new();
1026 for token in &tokens {
1027 let iter = match fts_table.get(token.as_str()) {
1028 Ok(iter) => iter,
1029 Err(_) => continue,
1030 };
1031 for result in iter {
1032 let node_id_guard = result?;
1033 let node_id = node_id_guard.value().to_string();
1034 *hit_counts.entry(node_id).or_insert(0) += 1;
1035 }
1036 }
1037
1038 // Sort by relevance: boost entity nodes (3x) and file nodes (2x)
1039 // over change nodes (1x). Entities and files are more useful for
1040 // code exploration than individual change records.
1041 let mut ranked: Vec<(String, usize)> = hit_counts.into_iter().collect();
1042 ranked.sort_by(|a, b| {
1043 let boost_a = if a.0.starts_with("entity:") {
1044 a.1 * 3
1045 } else if a.0.starts_with("file:") {
1046 a.1 * 2
1047 } else {
1048 a.1
1049 };
1050 let boost_b = if b.0.starts_with("entity:") {
1051 b.1 * 3
1052 } else if b.0.starts_with("file:") {
1053 b.1 * 2
1054 } else {
1055 b.1
1056 };
1057 boost_b.cmp(&boost_a)
1058 });
1059 ranked.truncate(limit);
1060
1061 // Fetch full nodes
1062 let mut nodes = Vec::new();
1063 for (id, _) in &ranked {
1064 if let Some(node) = self.get_kg_node(id)? {
1065 nodes.push(node);
1066 }
1067 }
1068 Ok(nodes)

Callers

nothing calls this directly

Calls 9

tokenize_for_ftsFunction · 0.85
cmpMethod · 0.80
truncateMethod · 0.80
getMethod · 0.65
is_emptyMethod · 0.45
as_strMethod · 0.45
into_iterMethod · 0.45
get_kg_nodeMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected