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

Method search_embeddings

atomic-core/src/pristine/txn/read.rs:1661–1712  ·  view source on GitHub ↗
(
        &self,
        query_vector: &[f32],
        top_k: usize,
    )

Source from the content-addressed store, hash-verified

1659 }
1660
1661 fn search_embeddings(
1662 &self,
1663 query_vector: &[f32],
1664 top_k: usize,
1665 ) -> PristineResult<Vec<SearchResult>> {
1666 let table = match self.txn.open_table(EMBEDDINGS) {
1667 Ok(table) => table,
1668 Err(redb::TableError::TableDoesNotExist(_)) => return Ok(Vec::new()),
1669 Err(e) => return Err(PristineError::from(e)),
1670 };
1671
1672 let mut scored: Vec<SearchResult> = Vec::new();
1673
1674 for item in table.iter()? {
1675 let (key_guard, value_guard) = item?;
1676 let key_str = key_guard.value();
1677
1678 let (path, chunk_idx) = match decode_embedding_key(key_str) {
1679 Some(decoded) => decoded,
1680 None => continue,
1681 };
1682
1683 let bytes = value_guard.value();
1684 let record: EmbeddingRecord =
1685 postcard::from_bytes(bytes).map_err(|e| PristineError::Serialization {
1686 message: format!(
1687 "failed to deserialize EmbeddingRecord at '{}': {}",
1688 key_str, e
1689 ),
1690 })?;
1691
1692 let score = cosine_similarity(query_vector, &record.vector);
1693
1694 scored.push(SearchResult {
1695 path: path.to_string(),
1696 chunk_idx,
1697 score,
1698 preview: record.preview,
1699 });
1700 }
1701
1702 // Sort by descending score
1703 scored.sort_by(|a, b| {
1704 b.score
1705 .partial_cmp(&a.score)
1706 .unwrap_or(std::cmp::Ordering::Equal)
1707 });
1708
1709 // Return top-k
1710 scored.truncate(top_k);
1711 Ok(scored)
1712 }
1713}
1714
1715// CrdtTxnT (read accessors) implementation for ReadTxn.

Callers 1

vault_searchMethod · 0.45

Calls 7

decode_embedding_keyFunction · 0.85
valueMethod · 0.80
truncateMethod · 0.80
cosine_similarityFunction · 0.70
iterMethod · 0.45
pushMethod · 0.45
partial_cmpMethod · 0.45

Tested by

no test coverage detected