咨询apoc.algo.dijkstra算法如何适配所有关系类型
apoc.algo.dijkstra Great question! The issue with your initial attempt is that you didn't wrap the * in quotes—APOC's apoc.algo.dijkstra expects the relationship type parameter to be a string pattern, not an unquoted wildcard. Here's how to fix it:
Correct Syntax to Match All Relationship Types
To include every relationship type (including any future ones you add), pass '*' as the relationship type argument. This wildcard pattern tells the algorithm to traverse any relationship between nodes, regardless of its type:
apoc.algo.dijkstra(startNode, endNode, '*', 'distance') YIELD path, weight
Why Your Original Attempt Failed
When you used * without quotes, Neo4j/APOC doesn't interpret it as a relationship wildcard—it's treated as invalid syntax or an unbound variable, which is why you got empty results. Wrapping it in quotes ensures it's recognized as a valid relationship pattern.
Bonus: Fine-Tuning Relationship Traversal
If you ever need to adjust the traversal (while still keeping most relationship types), you can use pattern modifiers:
- To traverse only outgoing relationships:
'*>' - To traverse only incoming relationships:
'<*' - To exclude specific relationship types:
'* EXCLUDE {KNOWS, WORKS_WITH}'
This approach will automatically include any new relationship types you add later, so you won't have to update your query every time the schema changes.
内容的提问来源于stack exchange,提问作者Artem Nazarenko

