Install
openclaw skills install multi-hop-reasoning-query-builderGenerate graph queries that perform multi-hop traversal and reasoning across relationships in graph databases and knowledge graphs.
openclaw skills install multi-hop-reasoning-query-builderConstruct graph queries that explore indirect relationships through multiple hops in a graph.
This skill helps developers generate queries that traverse several relationships to discover hidden connections and complex patterns within graph datasets.
Multi-hop reasoning is commonly used in:
The skill produces queries that traverse multiple edges to identify connections between entities.
Use this skill when a user wants to:
The skill generates graph queries capable of:
Outputs include:
Pattern: Traverse exactly N relationships
Cypher:
MATCH (start:$startLabel {id: $startId})-[:$relType*2]->(target)
RETURN target
Example - Friends of Friends:
MATCH (alice:Person {name:"Alice"})-[:FOLLOWS*2]->(fof:Person)
RETURN fof
Use Cases:
Pattern: Traverse between MIN and MAX relationships
Cypher:
MATCH (start:$startLabel {id: $startId})-[:$relType*1..$maxHops]->(target)
RETURN target
LIMIT $limit
Example - Up to 3 Hops:
MATCH (company:Company {id:"C123"})-[:SUPPLIES*1..3]->(supplier)
RETURN supplier
LIMIT 50
Use Cases:
Pattern: Return actual path structures
Cypher:
MATCH path = (start:$startLabel)-[*1..$maxHops]-(target:$targetLabel)
RETURN path, LENGTH(path) as hops
LIMIT $limit
Example - Path from Alice to Bob:
MATCH path = (alice:Person {name:"Alice"})-[*1..4]-(bob:Person {name:"Bob"})
RETURN path, LENGTH(path) as hops
LIMIT 10
Returns: Full traversal paths showing relationships
Pattern: Multi-hop with relationship type filtering
Cypher:
MATCH (start:$startLabel)-[:$relType*1..$hops]->(target:$targetLabel)
WHERE start.id = $startId AND target.$property = $value
RETURN target
LIMIT $limit
Example - Specific Relationship Chain:
MATCH (source:Person)-[:KNOWS|:WORKS_WITH*1..3]->(target:Person)
WHERE source.name = "Alice" AND target.active = true
RETURN target
LIMIT 25
Pattern: Follow relationships in any direction
Cypher:
MATCH (start:$startLabel)-[:$relType*1..$maxHops]-(target)
RETURN target
Example - Undirected Network:
MATCH (node:Entity)-[*1..3]-(connected)
RETURN connected
LIMIT 100
Pattern: SPARQL+ operator for one or more hops
SPARQL:
SELECT ?target
WHERE {
?start ex:$property+ ?target .
}
Example - Property Path:
PREFIX ex: <http://example.org/>
SELECT ?person
WHERE {
:Alice ex:knows+ ?person .
}
Pattern: Multi-hop with aggregation
Cypher:
MATCH (start:$startLabel)-[:$relType*1..$hops]->(target)
RETURN target, COUNT(*) as path_count
GROUP BY target
ORDER BY path_count DESC
Example - Count Paths:
MATCH (alice:Person {name:"Alice"})-[:FOLLOWS*1..3]->(person)
RETURN person, COUNT(*) as num_paths
GROUP BY person
ORDER BY num_paths DESC
LIMIT 10
Pattern: Multi-hop with WHERE conditions
Cypher:
MATCH path = (start:$startLabel)-[:$relType*1..$hops]->(target)
WHERE ALL(n in nodes(path) WHERE n.$property <> $excludeValue)
RETURN target
Example - Avoid Specific Nodes:
MATCH path = (alice:Person {name:"Alice"})-[:KNOWS*1..3]->(person)
WHERE ALL(n in nodes(path) WHERE n.status = "active")
RETURN person
LIMIT 50
Hop Depth Impact:
Depth 1: O(n)
├─ 1 hop from start node
└─ Direct relationships only
Depth 2: O(n²)
├─ 2 hops from start node
└─ Friends of friends
Depth 3: O(n³)
├─ 3 hops from start node
└─ Can be expensive
Depth 4+: O(n^4+) ⚠️ EXPENSIVE
├─ Exponential growth
└─ Use only with filtering/limits
Performance Guidelines:
MATCH (user:User {id: $userId})-[:FOLLOWS*2]->(fof:User)
WHERE NOT (user)-[:FOLLOWS]->(fof)
RETURN DISTINCT fof
LIMIT 50
MATCH (customer:Customer {id: $customerId})-[:PURCHASED*1..2]->(recommended:Product)
RETURN recommended, COUNT(*) as relevance
GROUP BY recommended
ORDER BY relevance DESC
LIMIT 10
MATCH (company:Company {name: $companyName})-[:SUPPLIES*1..3]->(supplier:Company)
RETURN supplier, COUNT(*) as connections
GROUP BY supplier
ORDER BY connections DESC
LIMIT 20
When building multi-hop queries:
Limit Traversal Depth
Restrict Relationship Types
Use Result Limits
Start from Specific Nodes
Filter Within Paths
Apply Indexes
Test Performance
Document Assumptions
This skill integrates with:
The skill returns:
Executable Cypher or SPARQL query with specified hop depth.
Returns relationship paths showing traversal chains.
Reusable template with hop parameters.
Performance metrics and recommendations.
Discover all entities within N hops.
MATCH (start)-[:RELATIONSHIP*1..$n]->(end)
RETURN end
Return actual paths showing relationship chains.
MATCH path = (start)-[*1..$n]-(end)
RETURN path
Count number of different paths.
MATCH (start)-[:REL*1..$n]->(end)
RETURN end, COUNT(*) as paths
Find shortest connection.
MATCH path = shortestPath((start)-[*]-(end))
RETURN path
Find cycles in relationships.
MATCH path = (start)-[:REL*2..]->(start)
RETURN path
This skill generates multi-hop graph queries that traverse multiple relationships to uncover indirect connections and hidden patterns in graph datasets.
It enables developers to perform advanced graph reasoning using Cypher or SPARQL for social networks, recommendation systems, fraud detection, and knowledge graph exploration.
Status: Enterprise-Grade Multi-Hop Reasoning
Comprehensive multi-hop query generation and reasoning system for professional knowledge graph development.