dopple
dopple

Reputation: 92

Generating a list of edges in neo4j from a variable length path

I am trying to generate a list of edges to read into an R dataframe for use in the vizNetwork package. This has to be, in its most simple format, as a table with 2 columns, "from" and "to".

In the neo4j console, the default query that pops up pretty much illustrates the type of query I am trying to run in that a starting point is specified then the resulting chain is the graph I'm interested in.

The query that is run is match (n:Crew)-[r:KNOWS*]-(m) where n.name='Neo' return n as Neo,r,m and this returns a graph showing the chain of relationships and also a table that I have included below.

Neo                   r                                                            m
(0:Crew {name:"Neo"}) [(0)-[0:KNOWS]->(1)]                                         (1:Crew {name:"Morpheus"})
(0:Crew {name:"Neo"}) [(0)-[0:KNOWS]->(1), (1)-[2:KNOWS]->(2)]                     (2:Crew {name:"Trinity"})
(0:Crew {name:"Neo"}) [(0)-[0:KNOWS]->(1), (1)-[3:KNOWS]->(3)]                     (3:Crew:Matrix {name:"Cypher"})
(0:Crew {name:"Neo"}) [(0)-[0:KNOWS]->(1), (1)-[3:KNOWS]->(3), (3)-[4:KNOWS]->(4)] (4:Matrix {name:"Agent Smith"})

The table doesn't contain the edges in a very "edgy" format. How can I make that relationship graph return the results in the following format...

from         to
Neo          Morpheus
Morpheus     Trinity
Morpheus     Cypher
Cypher       Agent Smith

Nicole White wrote a guide on integrating rneo4j and vizNetwork but this relied on generating the node list first then referencing the ids of the resulting list as a parameter in the edge list query. I was hoping to achieve a "pure cypher" method if possible.

Upvotes: 0

Views: 442

Answers (1)

Tezra
Tezra

Reputation: 8833

Each row you want to return is 1 edge. So to get the return you want, just match on the edge and Neo4j will create a row for every valid occurrence of that pattern.

MATCH (from)-[:KNOWS]->(to)
RETURN from.name as from, to.name as to

You can than filter that using WHERE pattern matching like so

MATCH (from)-[:KNOWS]->(to)
WHERE (:Person {name:"Neo"})-[:KNOWS*..25]->(from)
RETURN from.name as from, to.name as to

Upvotes: 2

Related Questions