pub struct KnowledgeGraph {
pub relationship_hierarchy: Option<RelationshipHierarchy>,
/* private fields */
}Expand description
Knowledge graph containing entities and their relationships
Fields§
§relationship_hierarchy: Option<RelationshipHierarchy>Hierarchical organization of relationships (Phase 3.1)
Implementations§
Source§impl KnowledgeGraph
impl KnowledgeGraph
Sourcepub fn add_document(&mut self, document: Document) -> Result<()>
pub fn add_document(&mut self, document: Document) -> Result<()>
Add a document to the knowledge graph
Sourcepub fn add_entity(&mut self, entity: Entity) -> Result<NodeIndex>
pub fn add_entity(&mut self, entity: Entity) -> Result<NodeIndex>
Add an entity to the knowledge graph
Sourcepub fn add_relationship(&mut self, relationship: Relationship) -> Result<()>
pub fn add_relationship(&mut self, relationship: Relationship) -> Result<()>
Add a relationship between entities
Sourcepub fn get_entity(&self, id: &EntityId) -> Option<&Entity>
pub fn get_entity(&self, id: &EntityId) -> Option<&Entity>
Get an entity by ID
Sourcepub fn get_document(&self, id: &DocumentId) -> Option<&Document>
pub fn get_document(&self, id: &DocumentId) -> Option<&Document>
Get a document by ID
Sourcepub fn get_entity_mut(&mut self, id: &EntityId) -> Option<&mut Entity>
pub fn get_entity_mut(&mut self, id: &EntityId) -> Option<&mut Entity>
Get a mutable reference to an entity by ID
Sourcepub fn get_chunk_mut(&mut self, id: &ChunkId) -> Option<&mut TextChunk>
pub fn get_chunk_mut(&mut self, id: &ChunkId) -> Option<&mut TextChunk>
Get a mutable reference to a chunk by ID
Sourcepub fn entities_mut(&mut self) -> impl Iterator<Item = &mut Entity>
pub fn entities_mut(&mut self) -> impl Iterator<Item = &mut Entity>
Get all entities (mutable)
Sourcepub fn documents_mut(&mut self) -> impl Iterator<Item = &mut Document>
pub fn documents_mut(&mut self) -> impl Iterator<Item = &mut Document>
Get all documents (mutable)
Sourcepub fn chunks_mut(&mut self) -> impl Iterator<Item = &mut TextChunk>
pub fn chunks_mut(&mut self) -> impl Iterator<Item = &mut TextChunk>
Get all chunks (mutable)
Sourcepub fn get_neighbors(
&self,
entity_id: &EntityId,
) -> Vec<(&Entity, &Relationship)>
pub fn get_neighbors( &self, entity_id: &EntityId, ) -> Vec<(&Entity, &Relationship)>
Get neighbors of an entity
Sourcepub fn get_all_relationships(&self) -> Vec<&Relationship>
pub fn get_all_relationships(&self) -> Vec<&Relationship>
Get all relationships in the graph
Sourcepub fn load_from_json(file_path: &str) -> Result<Self>
pub fn load_from_json(file_path: &str) -> Result<Self>
Load knowledge graph from JSON file
Sourcepub fn save_to_json(&self, file_path: &str) -> Result<()>
pub fn save_to_json(&self, file_path: &str) -> Result<()>
Save knowledge graph to JSON file with optimized format for entities and relationships
Sourcepub fn find_entities_by_name(&self, name: &str) -> impl Iterator<Item = &Entity>
pub fn find_entities_by_name(&self, name: &str) -> impl Iterator<Item = &Entity>
Find entities by name (case-insensitive partial match)
Sourcepub fn get_entity_by_id(&self, id: &str) -> Option<&Entity>
pub fn get_entity_by_id(&self, id: &str) -> Option<&Entity>
Get entity by ID (string version for compatibility)
Sourcepub fn get_entity_relationships(
&self,
entity_id: &str,
) -> impl Iterator<Item = &Relationship>
pub fn get_entity_relationships( &self, entity_id: &str, ) -> impl Iterator<Item = &Relationship>
Get entity relationships
Sourcepub fn find_relationship_path(
&self,
entity1: &str,
entity2: &str,
_max_depth: usize,
) -> Vec<String>
pub fn find_relationship_path( &self, entity1: &str, entity2: &str, _max_depth: usize, ) -> Vec<String>
Find relationship path between two entities (simplified BFS)
Sourcepub fn entity_count(&self) -> usize
pub fn entity_count(&self) -> usize
Count entities in the graph
Sourcepub fn relationship_count(&self) -> usize
pub fn relationship_count(&self) -> usize
Count relationships in the graph
Sourcepub fn document_count(&self) -> usize
pub fn document_count(&self) -> usize
Count documents in the graph
Sourcepub fn relationships(&self) -> impl Iterator<Item = &Relationship>
pub fn relationships(&self) -> impl Iterator<Item = &Relationship>
Get all relationships as an iterator
Sourcepub fn clear_entities_and_relationships(&mut self)
pub fn clear_entities_and_relationships(&mut self)
Clear all entities and relationships while preserving documents and chunks
This is useful for rebuilding the graph from scratch without reloading documents.
Sourcepub fn dynamic_weight(
&self,
relationship: &Relationship,
query_embedding: Option<&[f32]>,
query_concepts: &[String],
) -> f32
pub fn dynamic_weight( &self, relationship: &Relationship, query_embedding: Option<&[f32]>, query_concepts: &[String], ) -> f32
Calculate dynamic weight for a relationship based on query context (Phase 2.2)
Combines multiple factors:
- Base weight: relationship.confidence
- Semantic boost: similarity between relationship and query embeddings
- Temporal boost: relevance of temporal range
- Conceptual boost: matching query concepts in relationship type
§Arguments
relationship- The relationship to weightquery_embedding- Optional embedding vector for the queryquery_concepts- Concepts extracted from the query
§Returns
Dynamic weight value (typically 0.0-2.0, can be higher with strong matches)
Sourcepub async fn build_relationship_hierarchy(
&mut self,
num_levels: usize,
ollama_client: Option<OllamaClient>,
) -> Result<()>
pub async fn build_relationship_hierarchy( &mut self, num_levels: usize, ollama_client: Option<OllamaClient>, ) -> Result<()>
Build hierarchical organization of relationships (Phase 3.1)
Creates multi-level clusters of relationships using recursive community detection and LLM-generated summaries for efficient retrieval.
§Arguments
num_levels- Number of hierarchy levels to create (default: 3)ollama_client- Optional Ollama client for generating cluster summaries
§Returns
Result containing the built hierarchy or an error
§Example
use graphrag_core::{KnowledgeGraph, ollama::OllamaClient};
let mut graph = KnowledgeGraph::new();
// ... build graph ...
let ollama = OllamaClient::new("http://localhost:11434", "llama3.2");
graph.build_relationship_hierarchy(3, Some(ollama)).await.unwrap();Trait Implementations§
Source§impl Clone for KnowledgeGraph
impl Clone for KnowledgeGraph
Source§fn clone(&self) -> KnowledgeGraph
fn clone(&self) -> KnowledgeGraph
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for KnowledgeGraph
impl Debug for KnowledgeGraph
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more