In this cypher query,the longest path/paths between nodes which have relationship with STATUS="on" property with each other,will be returned,but I want to get also the last node of the path/paths.
query:
START n=node(*)
MATCH p=n-[rels:INCLUDE*]->m
WHERE ALL (rel IN rels
WHERE rel.status='on')
WITH COLLECT(p) AS paths, MAX(length(p)) AS maxLength
RETURN FILTER(path IN paths
WHERE length(path)= maxLength) AS longestPaths
how should I add it to the query? thanks.
The function coalesce() returns the first non- null value in the given list of expressions. Syntax: coalesce(expression [, expression]*) Returns: The type of the value returned will be that of the first non- null expression.
Return all elements When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.
With UNWIND , you can transform any list back into individual rows. These lists can be parameters that were passed in, previously collect -ed result or other list expressions. One common usage of unwind is to create distinct lists. Another is to create data from parameter lists that are provided to the query.
This would give two arrays. The first array is the last item in each path, the second is each path:
START n=node(*)
MATCH p=n-[rels:INCLUDE*]->m
WHERE ALL (rel IN rels
WHERE rel.status='on')
WITH COLLECT(p) AS paths, MAX(length(p)) AS maxLength
WITH FILTER(path IN paths WHERE length(path)= maxLength) AS longestPaths
RETURN EXTRACT(path IN longestPaths | LAST(path)) as last, longestPaths
Since a path is a collection you can apply the LAST
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With