Knowledge Graph Ranking Expansion Testing
Overview
The Knowledge Graph Ranking Expansion Test validates how adding synonyms to the knowledge graph dramatically improves search ranking performance. This test demonstrates the power of semantic connections in the Terraphim graph-based search system.
Test Location
File: crates/terraphim_middleware/tests/knowledge_graph_ranking_expansion_test.rs
Test Objectives
The test validates five core requirements:
- Knowledge Graph Construction - Build KG from
docs/src/kgmarkdown files - Node/Edge Counting - Measure graph structure before and after expansion
- Synonym Addition - Add new records with defined synonyms including target terms
- Structure Changes - Verify nodes/edges increased after expansion
- Ranking Validation - Confirm "terraphim-graph" rank changed using Terraphim Engineer role
Test Workflow
Phase 1: Setup Test Environment
// Create isolated temporary directory
let temp_dir = new.expect;
let temp_kg_path = temp_dir.path.join;
// Copy existing KG files to temp directory
// Files: terraphim-graph.md, service.md, haystack.mdPhase 2: Build Initial Knowledge Graph
let logseq_builder = default;
let initial_thesaurus = logseq_builder
.build
.await?;
let initial_rolegraph = new.await?;Initial State Metrics:
- Thesaurus: 10 terms extracted from 3 markdown files
- Graph Structure: 3 nodes, 5 edges, 3 documents
- Search Rank: "terraphim-graph" baseline rank measured
Phase 3: Add New Knowledge Graph Record
Create graph-analysis.md with comprehensive synonym set:
New Synonyms Added:
- data analysis
- network analysis
- graph processing
- relationship mapping
- connectivity analysis
- terraphim-graph (target term)
- graph embeddings
Phase 4: Rebuild and Measure Expansion
let expanded_thesaurus = logseq_builder
.build
.await?;
let expanded_rolegraph = new.await?;Phase 5: Validate Changes
Growth Verification:
assert!;
assert!;
assert!;
assert_ne!;Test Results
Dramatic Performance Improvement
| Metric | Initial | Expanded | Growth | |--------|---------|----------|--------| | Thesaurus Terms | 10 | 16 | +6 (+60%) | | Nodes | 3 | 4 | +1 (+33%) | | Edges | 5 | 8 | +3 (+60%) | | Documents | 3 | 4 | +1 (+33%) | | "terraphim-graph" Rank | 28 | 117 | +89 (+318%) |
Key Findings
- Synonym Power: Adding relevant synonyms creates +318% ranking improvement
- Graph Growth: Each new concept creates multiple semantic connections
- Search Enhancement: New terms become immediately discoverable
- Role Integration: Terraphim Engineer role properly utilizes local KG
- Ranking Algorithm: TerraphimGraph scoring rewards semantic richness
New Synonym Searchability
All 6 new synonyms are immediately searchable and return results:
- β "data analysis" β 1 result
- β "network analysis" β 1 result
- β "graph processing" β 1 result
- β "relationship mapping" β 1 result
- β "connectivity analysis" β 1 result
- β "graph embeddings" β 2 results
Technical Implementation
Key Components
- Logseq Builder: Extracts synonyms using
synonyms::syntax from markdown - RoleGraph: Manages graph structure with precise node/edge counting
- Terraphim Engineer Role: Uses TerraphimGraph relevance function
- TempDir: Provides isolated, safe testing environment
Graph Structure Access
// Precise counting methods
let nodes_count = rolegraph.nodes_map.len;
let edges_count = rolegraph.edges_map.len;
let documents_count = rolegraph.get_all_documents.count;Search Ranking Analysis
let results = rolegraph.query_graph?;
let rank = results.first
.map
.unwrap_or;Usage for Knowledge Graph Expansion
Strategy for Improving Search Rankings
- Identify Target Terms: Find important terms that should rank higher
- Research Synonyms: Discover related terms users might search for
- Create Knowledge Records: Add markdown files with synonym mappings
- Measure Impact: Use this test pattern to validate improvements
- Iterate: Refine synonym sets based on ranking improvements
Best Practices
- Relevant Synonyms: Use domain-appropriate terms users actually search for
- Semantic Connections: Connect related concepts through shared synonyms
- Measurement: Always measure before/after to validate improvements
- Testing: Use isolated environments to prevent production interference
Production Applications
Knowledge Graph Enhancement Workflow
- Baseline Measurement: Record current search rankings for key terms
- Synonym Research: Identify related terms and concepts
- Content Creation: Add markdown files with strategic synonym mappings
- Impact Validation: Measure ranking improvements using test framework
- Production Deployment: Apply successful synonym strategies to live KG
Performance Monitoring
Track these metrics when expanding knowledge graphs:
- Thesaurus Growth: Number of new terms added
- Graph Structure: Node and edge count increases
- Search Performance: Ranking improvements for target terms
- Discoverability: New synonym search success rates
Conclusion
The Knowledge Graph Ranking Expansion Test demonstrates that strategic synonym addition can create dramatic search ranking improvements. The test framework provides a reliable method for validating knowledge graph enhancements before production deployment.
Key Takeaway: Adding 6 relevant synonyms improved "terraphim-graph" search ranking by 318%, proving the power of semantic connections in graph-based search systems.