[英]Neo4j cypher query: AllShortestPaths with specified nodes and relationship properties
i'm new to neo4j. 我是Neo4j的新手。
I created this example: 我创建了以下示例:
// (Node2)
// / \
// Rel2 / \ Rel3
// / \
// (Node1) ------> (Node3)
// Rel1
//
// create nodes
CREATE
(n1:Node1{id:1,rights:["admin","user"]}),
(n2:Node2{id:2,rights:["admin","user"]}),
(n3:Node3{id:3,rights:["admin","user"]})
// create relationships
CREATE (n1)-[r1:Rel1{id:11,rights:["admin"]}]->(n3)
CREATE (n1)-[r2:Rel2{id:12,rights:["admin","user"]}]->(n2)
CREATE (n2)-[r3:Rel3{id:13,rights:["admin","user"]}]->(n3)
RETURN n1,n2,n3,r1,r2,r3
Each node and relationship has a property array with some rights values. 每个节点和关系都有一个带有一些权限值的属性数组。 I want to create a query that gives me (shortest) paths between two nodes with specified properties.
我想创建一个查询,该查询为我提供具有指定属性的两个节点之间的(最短)路径。 Example:
例:
User has rights 'admin', the path should be: 用户具有“管理员”权限,路径应为:
(Node1)-[Rel1]->(Node3) (节点1)-[角色1]->(节点3)
(Each node and releationship has the 'admin' String in the 'rights' property.) (每个节点和关系在'rights'属性中都有'admin'字符串。)
If the user has the right 'user', the path should be: 如果用户具有正确的“用户”,则路径应为:
(Node1)-[Rel2]-(Node2)-[Rel3]-(Node3) (节点1)-[角色2]-(节点2)-[角色3]-(节点3)
(Because the Rel1 relation didn't have the 'user' String in the 'rights' property.) (因为Rel1关系在'rights'属性中没有'user'字符串。)
At first i tried this query that worked: 最初,我尝试了这个有效的查询:
WITH ["admin","otherRight"] AS usersRights
MATCH path=allShortestPaths((n1:Node1{id:1})-[*..4]-(n2:Node3{id:3}))
WITH *, relationships(path) AS rels, nodes(path) as nodes
WHERE ANY (rel IN rels WHERE ANY(r IN rel.rights WHERE r IN usersRights))
AND ANY (node IN nodes WHERE ANY(r IN node.rights WHERE r IN usersRights))
RETURN path
Then i replaced the 'admin' with 'user', but this query didn't work (has no rusults): 然后,我用“用户”替换了“管理员”,但是此查询不起作用(没有鲁):
WITH ["user","otherRight"] AS usersRights
MATCH ... // same as above
...
RETURN path
The follwing query matches my needed result, but in this case, i would have to make several queries to get the desired result (shortest paths) for depth n. 以下查询与我所需的结果匹配,但是在这种情况下,我将不得不进行几次查询才能获得深度n的所需结果(最短路径)。
WITH ["user","otherRight"] AS usersRights
MATCH path=(n1:Node1{id:1})-[r1]-(n2)-[r2]-(n3:Node3{id:3})
WHERE ANY(r IN n1.rights WHERE r IN usersRights)
AND ANY(r IN r1.rights WHERE r IN usersRights)
AND ANY(r IN n2.rights WHERE r IN usersRights)
AND ANY(r IN r2.rights WHERE r IN usersRights)
AND ANY(r IN n3.rights WHERE r IN usersRights)
RETURN path
Is it possible to create a cypher query that gives me the disired results ? 是否可以创建一个密码查询,使我得到不良结果?
Thanks. 谢谢。
Your first query has a WITH
clause separating the MATCH
and WHERE
clauses, which prevents the WHERE
clause from affecting the behavior of the allshortestpath()
function. 您的第一个查询具有一个
WITH
子句,该子句将MATCH
和WHERE
子句分隔开,从而防止WHERE
子句影响allshortestpath()
函数的行为。 Therefore, the function was just returning the shortest path(s) without testing the nodes and relationships. 因此,该函数仅返回最短路径,而不测试节点和关系。
Try this, instead: 试试这个,代替:
WITH ["admin","otherRight"] AS usersRights
MATCH path=allShortestPaths((n1:Node1{id:1})-[*..4]-(n2:Node3{id:3}))
WHERE
ANY (rel IN relationships(path) WHERE ANY(r IN rel.rights WHERE r IN usersRights)) AND
ANY (node IN nodes(path) WHERE ANY(r IN node.rights WHERE r IN usersRights))
RETURN path;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.