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

Method kg_fts_search

atomic-core/src/pristine/txn/write/triples.rs:236–294  ·  view source on GitHub ↗
(&self, query: &str, limit: usize)

Source from the content-addressed store, hash-verified

234 }
235
236 fn kg_fts_search(&self, query: &str, limit: usize) -> PristineResult<Vec<KgNode>> {
237 let fts_table = match self.txn.open_multimap_table(KG_FTS) {
238 Ok(t) => t,
239 Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()),
240 Err(e) => return Err(PristineError::from(e)),
241 };
242
243 let tokens = tokenize_for_fts(query);
244 if tokens.is_empty() {
245 return Ok(Vec::new());
246 }
247
248 // Collect node IDs that match any token, count matches per node
249 let mut hit_counts: std::collections::HashMap<String, usize> =
250 std::collections::HashMap::new();
251 for token in &tokens {
252 let iter = match fts_table.get(token.as_str()) {
253 Ok(iter) => iter,
254 Err(_) => continue,
255 };
256 for result in iter {
257 let node_id_guard: redb::AccessGuard<'_, &str> = result?;
258 let node_id = node_id_guard.value().to_string();
259 *hit_counts.entry(node_id).or_insert(0) += 1;
260 }
261 }
262
263 // Sort by relevance: boost entity nodes (3x) and file nodes (2x)
264 // over change nodes (1x). Entities and files are more useful for
265 // code exploration than individual change records.
266 let mut ranked: Vec<(String, usize)> = hit_counts.into_iter().collect();
267 ranked.sort_by(|a, b| {
268 let boost_a = if a.0.starts_with("entity:") {
269 a.1 * 3
270 } else if a.0.starts_with("file:") {
271 a.1 * 2
272 } else {
273 a.1
274 };
275 let boost_b = if b.0.starts_with("entity:") {
276 b.1 * 3
277 } else if b.0.starts_with("file:") {
278 b.1 * 2
279 } else {
280 b.1
281 };
282 boost_b.cmp(&boost_a)
283 });
284 ranked.truncate(limit);
285
286 // Fetch full nodes
287 let mut nodes = Vec::new();
288 for (id, _) in &ranked {
289 if let Some(node) = self.get_kg_node(id)? {
290 nodes.push(node);
291 }
292 }
293 Ok(nodes)

Callers 1

test_kg_fts_searchFunction · 0.45

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 1

test_kg_fts_searchFunction · 0.36