Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-03-2021 05:25 AM
MERGE (Z1:Z {z_id : 'z1'})-[:HAS_CHILD]->(z2:Z {z_id : 'z2'})-[:HAS_CHILD]->(z3:Z {z_id : 'z3'})-[:HAS_CHILD]->(z4:Z {z_id : 'z4'})-[:HAS_CHILD]->(z5:Z {z_id : 'z5'})
Create (x1:X {x_id: 'x1'})-[:HAS_OBJ]->(y1: Y {y_id: 'y1'})-[:HAS_OBJ]->(x2:X {x_id: 'x2'})-[:HAS_OBJ]->(y2: Y {y_id: 'y2'})-[:HAS_OBJ]->(x3:X {x_id: 'x3'});
MATCH (y:Y {y_id: 'y1'}),(z:Z {z_id: 'z2'}) WITH y,z CREATE (y)-[:HAS_Z {status: true}]->(z);
MATCH (y:Y {y_id: 'y2'}),(z:Z {z_id: 'z2'}) WITH y,z CREATE (y)-[:HAS_Z {status: true}]->(z);
MATCH (y:Y {y_id: 'y2'}),(z:Z {z_id: 'z4'}) WITH y,z CREATE (y)-[:HAS_Z {status: false}]->(z);
Say, in the example above & considering x1 as startNode & x2 s endNode ...
This was the first version of my Cypher query, but it's not working as expected (returns path in case of z: z5)
MATCH (z:Z) WHERE z.z_id = 'z5'
WITH z MATCH (start:X) WHERE start.x_id = 'x1'
with z,start MATCH (end:X) WHERE end.x_id = 'x3'
WITH z,start,end
MATCH p=(start)-[:HAS_OBJ*1..]->(y:Y)-[:HAS_OBJ*1..]->(end), p2=shortestPath((y)-[*1..]->(z))
WHERE ALL (
r in relationships(p2) WHERE
type(r)= 'HAS_CHILD' OR (type(r) = 'HAS_Z' AND r.status = true)
)
return p
Thanks in advance
12-05-2021 05:32 AM
I think I found the solution (by adding one extra line: WITH p,p2
between the last MATCH and WHERE clause) - letting the shortest-path function first find the existing shortest-path available (& not searching for a (shortest-)path which is compatible with predicate)
MATCH (z:Z) WHERE z.z_id = 'z5'
WITH z MATCH (start:X) WHERE start.x_id = 'x1'
with z,start MATCH (end:X) WHERE end.x_id = 'x3'
WITH z,start,end
MATCH p=(start)-[:HAS_OBJ*1..]->(y:Y)-[:HAS_OBJ*1..]->(end), p2=shortestPath((y)-[*1..]->(z))
WITH p,p2
WHERE ALL (
r in relationships(p2) WHERE
type(r)= 'HAS_CHILD' OR (type(r) = 'HAS_Z' AND r.status = true)
)
return p,p2
All the sessions of the conference are now available online