pub struct KnowledgeGraph { /* private fields */ }Expand description
Knowledge Graph Manager - main entry point for the library.
Implementations§
Source§impl KnowledgeGraph
impl KnowledgeGraph
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open a new knowledge graph database connection.
Sourcepub fn open_in_memory() -> Result<Self>
pub fn open_in_memory() -> Result<Self>
Open an in-memory knowledge graph (useful for testing).
Sourcepub fn connection(&self) -> &Connection
pub fn connection(&self) -> &Connection
Get a reference to the underlying SQLite connection.
Sourcepub fn transaction(&self) -> Result<Transaction<'_>>
pub fn transaction(&self) -> Result<Transaction<'_>>
Begin a transaction for batch operations.
Sourcepub fn insert_entity(&self, entity: &Entity) -> Result<i64>
pub fn insert_entity(&self, entity: &Entity) -> Result<i64>
Insert an entity into the knowledge graph.
Sourcepub fn get_entity(&self, id: i64) -> Result<Entity>
pub fn get_entity(&self, id: i64) -> Result<Entity>
Get an entity by ID.
Sourcepub fn list_entities(
&self,
entity_type: Option<&str>,
limit: Option<i64>,
) -> Result<Vec<Entity>>
pub fn list_entities( &self, entity_type: Option<&str>, limit: Option<i64>, ) -> Result<Vec<Entity>>
List entities with optional filtering.
Sourcepub fn update_entity(&self, entity: &Entity) -> Result<()>
pub fn update_entity(&self, entity: &Entity) -> Result<()>
Update an entity.
Sourcepub fn delete_entity(&self, id: i64) -> Result<()>
pub fn delete_entity(&self, id: i64) -> Result<()>
Delete an entity.
Sourcepub fn insert_relation(&self, relation: &Relation) -> Result<i64>
pub fn insert_relation(&self, relation: &Relation) -> Result<i64>
Insert a relation between entities.
Sourcepub fn get_neighbors(&self, entity_id: i64, depth: u32) -> Result<Vec<Neighbor>>
pub fn get_neighbors(&self, entity_id: i64, depth: u32) -> Result<Vec<Neighbor>>
Get neighbors of an entity using BFS traversal.
Sourcepub fn insert_vector(&self, entity_id: i64, vector: Vec<f32>) -> Result<()>
pub fn insert_vector(&self, entity_id: i64, vector: Vec<f32>) -> Result<()>
Insert a vector embedding for an entity.
Sourcepub fn search_vectors(
&self,
query: Vec<f32>,
k: usize,
) -> Result<Vec<SearchResult>>
pub fn search_vectors( &self, query: Vec<f32>, k: usize, ) -> Result<Vec<SearchResult>>
Search for similar entities using vector embeddings.
Sourcepub fn create_turboquant_index(
&self,
config: Option<TurboQuantConfig>,
) -> Result<TurboQuantIndex>
pub fn create_turboquant_index( &self, config: Option<TurboQuantConfig>, ) -> Result<TurboQuantIndex>
Create a TurboQuant index for fast approximate nearest neighbor search.
TurboQuant provides:
- Instant indexing (no training required)
- 6x memory compression
- Near-zero accuracy loss
§Arguments
config- Optional configuration (uses defaults if None)
§Example
let config = TurboQuantConfig {
dimension: 384,
bit_width: 3,
seed: 42,
};
let mut index = kg.create_turboquant_index(Some(config))?;
// Add vectors to index
for (entity_id, vector) in all_vectors {
index.add_vector(entity_id, &vector)?;
}
// Fast search
let results = index.search(&query_vector, 10)?;Sourcepub fn build_turboquant_index(
&self,
config: Option<TurboQuantConfig>,
) -> Result<TurboQuantIndex>
pub fn build_turboquant_index( &self, config: Option<TurboQuantConfig>, ) -> Result<TurboQuantIndex>
Build a TurboQuant index from all existing vectors in the database. This is a convenience method that loads all vectors and indexes them.
Sourcepub fn kg_semantic_search(
&self,
query_embedding: Vec<f32>,
k: usize,
) -> Result<Vec<SearchResultWithEntity>>
pub fn kg_semantic_search( &self, query_embedding: Vec<f32>, k: usize, ) -> Result<Vec<SearchResultWithEntity>>
Semantic search using vector embeddings. Returns entities sorted by similarity score.
Sourcepub fn kg_get_context(&self, entity_id: i64, depth: u32) -> Result<GraphContext>
pub fn kg_get_context(&self, entity_id: i64, depth: u32) -> Result<GraphContext>
Get context around an entity using graph traversal. Returns neighbors up to the specified depth.
Sourcepub fn kg_hybrid_search(
&self,
_query_text: &str,
query_embedding: Vec<f32>,
k: usize,
) -> Result<Vec<HybridSearchResult>>
pub fn kg_hybrid_search( &self, _query_text: &str, query_embedding: Vec<f32>, k: usize, ) -> Result<Vec<HybridSearchResult>>
Hybrid search combining semantic search and graph context. Performs semantic search first, then retrieves context for top-k results.
Sourcepub fn kg_bfs_traversal(
&self,
start_id: i64,
direction: Direction,
max_depth: u32,
) -> Result<Vec<TraversalNode>>
pub fn kg_bfs_traversal( &self, start_id: i64, direction: Direction, max_depth: u32, ) -> Result<Vec<TraversalNode>>
BFS traversal from a starting entity. Returns all reachable entities within max_depth with depth information.
Sourcepub fn kg_dfs_traversal(
&self,
start_id: i64,
direction: Direction,
max_depth: u32,
) -> Result<Vec<TraversalNode>>
pub fn kg_dfs_traversal( &self, start_id: i64, direction: Direction, max_depth: u32, ) -> Result<Vec<TraversalNode>>
DFS traversal from a starting entity. Returns all reachable entities within max_depth.
Sourcepub fn kg_shortest_path(
&self,
from_id: i64,
to_id: i64,
max_depth: u32,
) -> Result<Option<TraversalPath>>
pub fn kg_shortest_path( &self, from_id: i64, to_id: i64, max_depth: u32, ) -> Result<Option<TraversalPath>>
Find shortest path between two entities using BFS. Returns the path with all intermediate steps (if exists).
Sourcepub fn kg_graph_stats(&self) -> Result<GraphStats>
pub fn kg_graph_stats(&self) -> Result<GraphStats>
Compute graph statistics.
Sourcepub fn kg_pagerank(
&self,
config: Option<PageRankConfig>,
) -> Result<Vec<(i64, f64)>>
pub fn kg_pagerank( &self, config: Option<PageRankConfig>, ) -> Result<Vec<(i64, f64)>>
Compute PageRank scores for all entities. Returns a vector of (entity_id, score) sorted by score descending.
Sourcepub fn kg_louvain(&self) -> Result<CommunityResult>
pub fn kg_louvain(&self) -> Result<CommunityResult>
Detect communities using Louvain algorithm. Returns community memberships and modularity score.
Sourcepub fn kg_connected_components(&self) -> Result<Vec<Vec<i64>>>
pub fn kg_connected_components(&self) -> Result<Vec<Vec<i64>>>
Find connected components in the graph. Returns a list of components, each being a list of entity IDs.
Sourcepub fn kg_analyze(&self) -> Result<GraphAnalysis>
pub fn kg_analyze(&self) -> Result<GraphAnalysis>
Run full graph analysis (PageRank + Louvain + Connected Components).