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 insert_hyperedge(&self, hyperedge: &Hyperedge) -> Result<i64>
pub fn insert_hyperedge(&self, hyperedge: &Hyperedge) -> Result<i64>
Insert a hyperedge (higher-order relation) into the knowledge graph.
Sourcepub fn get_hyperedge(&self, id: i64) -> Result<Hyperedge>
pub fn get_hyperedge(&self, id: i64) -> Result<Hyperedge>
Get a hyperedge by ID.
Sourcepub fn list_hyperedges(
&self,
hyperedge_type: Option<&str>,
min_arity: Option<usize>,
max_arity: Option<usize>,
limit: Option<i64>,
) -> Result<Vec<Hyperedge>>
pub fn list_hyperedges( &self, hyperedge_type: Option<&str>, min_arity: Option<usize>, max_arity: Option<usize>, limit: Option<i64>, ) -> Result<Vec<Hyperedge>>
List hyperedges with optional filtering.
Sourcepub fn update_hyperedge(&self, hyperedge: &Hyperedge) -> Result<()>
pub fn update_hyperedge(&self, hyperedge: &Hyperedge) -> Result<()>
Update a hyperedge.
Sourcepub fn delete_hyperedge(&self, id: i64) -> Result<()>
pub fn delete_hyperedge(&self, id: i64) -> Result<()>
Delete a hyperedge by ID.
Sourcepub fn get_higher_order_neighbors(
&self,
entity_id: i64,
min_arity: Option<usize>,
max_arity: Option<usize>,
) -> Result<Vec<HigherOrderNeighbor>>
pub fn get_higher_order_neighbors( &self, entity_id: i64, min_arity: Option<usize>, max_arity: Option<usize>, ) -> Result<Vec<HigherOrderNeighbor>>
Get higher-order neighbors of an entity (connected through hyperedges).
Sourcepub fn get_entity_hyperedges(&self, entity_id: i64) -> Result<Vec<Hyperedge>>
pub fn get_entity_hyperedges(&self, entity_id: i64) -> Result<Vec<Hyperedge>>
Get all hyperedges that an entity participates in.
Sourcepub fn kg_higher_order_bfs(
&self,
start_id: i64,
max_depth: u32,
min_arity: Option<usize>,
) -> Result<Vec<TraversalNode>>
pub fn kg_higher_order_bfs( &self, start_id: i64, max_depth: u32, min_arity: Option<usize>, ) -> Result<Vec<TraversalNode>>
Higher-order BFS traversal through hyperedges.
Sourcepub fn kg_higher_order_shortest_path(
&self,
from_id: i64,
to_id: i64,
max_depth: u32,
) -> Result<Option<HigherOrderPath>>
pub fn kg_higher_order_shortest_path( &self, from_id: i64, to_id: i64, max_depth: u32, ) -> Result<Option<HigherOrderPath>>
Find shortest path between two entities through hyperedges.
Sourcepub fn kg_hyperedge_degree(&self, entity_id: i64) -> Result<f64>
pub fn kg_hyperedge_degree(&self, entity_id: i64) -> Result<f64>
Compute hyperedge degree centrality for an entity.
Sourcepub fn kg_hypergraph_entity_pagerank(
&self,
damping: Option<f64>,
max_iter: Option<usize>,
tolerance: Option<f64>,
) -> Result<HashMap<i64, f64>>
pub fn kg_hypergraph_entity_pagerank( &self, damping: Option<f64>, max_iter: Option<usize>, tolerance: Option<f64>, ) -> Result<HashMap<i64, f64>>
Compute entity-level hypergraph PageRank using Zhou formula.
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).
Sourcepub fn export_json(&self) -> Result<D3ExportGraph>
pub fn export_json(&self) -> Result<D3ExportGraph>
Export the knowledge graph in D3.js JSON format.
Returns a D3ExportGraph containing nodes, links, and metadata,
ready for use with D3.js force-directed graph visualizations.
§Example
let graph = kg.export_json()?;
let json = serde_json::to_string_pretty(&graph)?;
std::fs::write("graph.json", json)?;Sourcepub fn export_dot(&self, config: &DotConfig) -> Result<String>
pub fn export_dot(&self, config: &DotConfig) -> Result<String>
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for KnowledgeGraph
impl !RefUnwindSafe for KnowledgeGraph
impl Send for KnowledgeGraph
impl !Sync for KnowledgeGraph
impl Unpin for KnowledgeGraph
impl UnsafeUnpin for KnowledgeGraph
impl !UnwindSafe for KnowledgeGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.