简体   繁体   中英

Returning edge list from Neo4j cypher query

Hi I have recently started using Neo and wanted to know how I can query a graph to return two columns of IDs showing connections.

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN s1.StudentID, s2.StudentId

The above gives me all the connection for s1 however It doesn't show any other connections. How can I collect all of the connections in this graph into one edge list? The plan is to use this query in RNeo4j and analyse with igraph.

Thanks in advance

If you are literally just interested in the pairs of nodes in each path then you could do something like the following.

This query takes the matched paths, groups the pairs of nodes in each path and then only returns the distinct pairs.

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01'
WITH REDUCE(pairs =[],r in relationships(path) | pairs + [[startNode(r).StudentId,endNode(r).StudentId]]) as pairs
UNWIND pairs as pair
WITH DISTINCT pair
RETURN pair[0] as from, pair[1] as to

The easiest way to do this is to return the path

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN path

As you are probably not interested in returning :FRIENDS relationship, because they are all the same you can return only nodes of the path

MATCH path=(s1:Student{StudentId: 7079945})-[r:FRIENDS*6..9]->(s2:Student)
WHERE  s1.StartDate> '2015-01-01' 
RETURN nodes(path)

You can find more functions for path in documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM