cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Expand nodes around a path of nodes

Hi All,

Suppose I have this path in my graph, and want to return this path.

I have written the below query but if I write this I am not getting the Capacity Node. I want to expand the node connected to the Production Node.


match p=(:WIPCapacity)-[:throughput]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)
return p;

Please let me know if you can suggest how to write the cypher query to get the full path.

Thanks,
Pragya

1 ACCEPTED SOLUTION

Here is my result:
I created  this:
merge (a:WIPProduction {name: "WIPP1"})
merge (b:WIPCapacity {name: "CAP1"})

merge (c1:WIPStorage {name: "STOR1"})
merge (c2:WIPStorage {name: "STOR2"})

merge (a1:Production {name: "P1"})

merge (b)-[:throughput]->(a)
merge (a)-[:transport]->(c1)
merge (c1)-[:transport]->(c2)
merge (c2)-[:transport]->(a1)

merge (b1:Capacity {name: "CAP2"})
merge (b1)-[:throughput]->(a1)

merge (c3:Storage {name: "STOR3"})
merge (c4:Storage {name: "STOR4"})
merge (c5:Storage {name: "STOR5"})

merge (a1)-[:transport]->(c3)
merge (c3)-[:transport]->(c4)
merge (c4)-[:transport]->(c5)

Result:
3X_8_9_89bbdd9898855b2f46865b9142c5b0087b691b0c.png

Ran this query:
MATCH (p:Capacity {name: "CAP2"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: '<transport|throughput',
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;

Result:
3X_1_d_1d3b401971c66222befb0e0793b927b06b9b52ba.png

View solution in original post

7 REPLIES 7

ameyasoft
Graph Maven
Try this:

MATCH (a:WIPCapacity)

//Add this to select a specific node.....
//WHERE a.name = "CAP1"
//End....

CALL apoc.path.spanningTree(a, {}) YIELD path
return path

Hi Ameya,

Thanks
This works and it gives me the full path, but if I know the name of Capacity Node, and want to return the Node WIP Capacity which is connected to that so for this I tried something similar like you did using apoc.

I tried this, but its not returning the WIPCapacity, its giving Capacity Node only in output:

MATCH (p:Capacity {name: "CAP1"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: "transport|throughput", labelFilter: "WIPCapacity"
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;

Try this:

MATCH (p:Capacity {name: "CAP1"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: '<transport|throughput',
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;
Change the relationshipFilter to this to go towards 'Storage' nodes. '<' indicates 'incoming' diretction
relationshipFilter: 'transport|throughput'

I tried this, still its returning the Capacity Node in the output and not giving me the WIPCapacity Node which is expected.

Here is my result:
I created  this:
merge (a:WIPProduction {name: "WIPP1"})
merge (b:WIPCapacity {name: "CAP1"})

merge (c1:WIPStorage {name: "STOR1"})
merge (c2:WIPStorage {name: "STOR2"})

merge (a1:Production {name: "P1"})

merge (b)-[:throughput]->(a)
merge (a)-[:transport]->(c1)
merge (c1)-[:transport]->(c2)
merge (c2)-[:transport]->(a1)

merge (b1:Capacity {name: "CAP2"})
merge (b1)-[:throughput]->(a1)

merge (c3:Storage {name: "STOR3"})
merge (c4:Storage {name: "STOR4"})
merge (c5:Storage {name: "STOR5"})

merge (a1)-[:transport]->(c3)
merge (c3)-[:transport]->(c4)
merge (c4)-[:transport]->(c5)

Result:
3X_8_9_89bbdd9898855b2f46865b9142c5b0087b691b0c.png

Ran this query:
MATCH (p:Capacity {name: "CAP2"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: '<transport|throughput',
    minLevel: 0,
    maxLevel: 8
})
YIELD node
RETURN node;

Result:
3X_1_d_1d3b401971c66222befb0e0793b927b06b9b52ba.png

Thank You for the explanation, I am also getting this correct till here but instead of this full path that we get in result can I just get the CAP1 node and its properties ?
Is there a way to put where condition, as my main goal is to check the properties of CAP1 and CAP2 which are on the same path, and compare the value of a particular property, then return the node where the value of a particular property( Required_Capacity) is greater.

I hope I am able to explain my use case properly.

Thanks,
Pragya

MATCH (p {name: "CAP2"})
CALL apoc.path.subgraphNodes(p, {
    relationshipFilter: "<transport|throughput", 
    minLevel: 0,
    maxLevel: 8
})
YIELD node
where node.Available_Capacity<node.Required_Capacity 
return node.name, node.Required_Capacity-node.Available_Capacity as d order by d desc;

I tried this putting where condition and returning the desired propeties and this seems to give me the correct output.

I hope this implementation is correct and we can add further steps if required in this query.