Search code examples
neo4jcypherneo4j-ogmneo4j-ogm-bolt-driver

Neo4j : return relationship between nodes in a path


As per my schema, I have a "Project" node and multiple "Revision" nodes connected to project. Every other node is connected to "Revision" nodes applicable. I need to fetch all nodes that are connected to a particular "Revision" node. While fetching nodes, I need to get the relationship between these nodes too. I need to restrict the nodes connected to a particular revision.

I tried below query, however, it degrades the performance as DB hits are more while profiling. Every revision will have around 28k nodes and 76k relationships between them.

MATCH (a:Project{name:{0}})-[h:HAS_REVISION]->(r:Revision)
WITH a
MATCH p=(a)-[h]->(r)-[*0..2]->(allRelatedNodes)
WHERE r.revisionNo={1} AND (r)-[]->(allRelatedNodes)
RETURN a, relationships(p), nodes(p)  

below query is cost effective. I get expected results while querying the in-browser database. However, while executing from my Java application, relations between nodes connected to the particular "Revision" are not fetched.

PROFILE  
MATCH (project:Project {name> :"test_local"})-[:HAS_REVISION]-
(revision:Revision{revisionNo:1})
WITH project,revision
MATCH p = (revision)-[]-(allRelatedNodes)
WITH project,revision,collect(p) as rs
RETURN project,revision,rs  

Can anyone please help.


Solution

  • I got this resolved using apoc.path.subgraphAll procedure. my requirement was that "i need to get all nodes connected to revision and also relations between those nodes connected to revision node". second query gave desired result in neo4j browser ui, but relations b/w nodes were not getting mapped in java neo4j entities. Finally this is what I did, match (project:Project {name :{0}})-[:HAS_REVISION]- " + "(revision:Revision{revisionNo:{1}}) " + "call apoc.path.subgraphAll(revision,{maxLevel:1}) yield nodes,relationships " + "return nodes,relationships;
    manually copied apoc jar in the plugins folder of neo4j. Thanks everybody for your suggestions.