Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-26-2020 06:27 AM
I need to build a short route with three different link conditions. For example, the first condition is the connection between nodes: (:n) - [:r] - (:n) is direct. The second condition is that the connection between nodes goes through an intermediate node: :t (:n) - [:rt] - (:t) - [:rt] - (:n). the Third condition is the connection between nodes through two intermediate nodes: t1 and: t2 (:n) - [:rt1] - (:t1) - [:rt1] - (:t2) - [:rt2] - (:n). In all three cases, the types of links are also different. Connections can be made in any sequence several times. How do I write the correct cypher query?
Thank you.
01-26-2020 09:49 AM
MATCH p=(start:NodeLabel{prop:"propValue"})-[r*1..3]->(end:NodeLabel{prop:propValue})
WHERE ALL(rel In r WHERE type(r) IN ["rt","rt1","rt2"])
WITH start,end, [rel In r| rel.weight] as weightList,nodes(p) as path
WITH start,end ,path, apoc.coll.sum(weightList) as distance
RETURN start.name as startNode, end.name as endNode,[x in path|x.name] as shortestRoute , distance ORDER BY distance LIMIT 1
this query searches for all the routes from start Node to end node and then finds the sum of weights on relations in the path and finally ordering by distance gives the shortest weighted path..if there is no weight , you can just use
SIZE(r) as distance in the place of apoc.coll.sum(weightList) as distance
01-26-2020 12:57 PM
It works thanks!!! How can I now add a condition inside ALL? For example, ALL (rel In r WHERE type(r) = "rt1" or type (r) = "rt" and not exists (match startnode(r) - [:rt1] - () )
I need [:rt] to be selected only if there is not exists [:rt1] from the same node
All the sessions of the conference are now available online