how to get the last node in path in neo4j?
Asked Answered
S

5

11

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.

Secunda answered 4/11, 2013 at 16:37 Comment(0)
C
8

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
Casie answered 4/11, 2013 at 21:3 Comment(2)
@Casie is there any way to get the last node instead of path I am getting some collectons of JSON instead of node?Beckmann
to translate this into Neo4j 2.x, change LAST(path) to LAST(nodes(path)) from the 2.x comments below. Anyways this is what worked for me.Nucleoprotein
M
3

Since a path is a collection you can apply the LAST function.

Mouth answered 4/11, 2013 at 18:16 Comment(2)
As of neo4j 2.0, a path is not a collection, and you can't run LAST on it directly.Fridafriday
you can use last(nodes(mypath))Mouth
D
1

This example is for getting the last node from every branch of nodes
connected with a next_action relations

MATCH p=(a:acct)-[:next_action*]->(c)
WITH COLLECT({node:c, l:length(p)}) AS paths, MAX(length(p)) AS maxLength, a.uid as uid
WITH [x IN paths WHERE x.l= maxLength] AS last_node
RETURN last_node

there is something a little strange, running this query in the Neo4j GUI interface will bring all the last nodes. Running the same query from py2neu will not. When running from py2neo the following modification will work

MATCH p=(a:acct)-[:next_action*]->(c)
WITH COLLECT({node:c, l:length(p)}) AS paths, MAX(length(p)) AS maxLength, a.uid as uid
WITH [x IN paths WHERE x.l= maxLength] AS last_node
WITH COLLECT(last_node) as last_nodes
RETURN last_nodes
Datolite answered 7/2, 2019 at 20:59 Comment(0)
C
0

As of Neo4J 4.X FILTER() is not longer supported so it can be replaced with REDUCE(). Here's how ::

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
WITH REDUCE( newPaths = [], path in paths |
            CASE
            WHEN length(path)= maxLength THEN newPaths + [path]
            END ) AS longestPaths
RETURN EXTRACT(path IN longestPaths | LAST(path)) as last, longestPaths
Cyclops answered 18/6, 2021 at 10:8 Comment(0)
I
0

For anyone here simply looking to get the last node in a path:

If last node has same type:

MATCH (c:Tweet{ id: 1 })-[:REPLY_TO*]->(target)
RETURN last(collect(target))

If last node has different type:

MATCH (c:Comment{ id: 1 })-[:COMMENT_TO*]->(post:HeychainPost)
RETURN post
Insure answered 4/4, 2023 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.