Find the position of nodes in a path

Hi,

I am looking to find a way to get the position of the nodes in my path starting from 1 to n.
For example, Below is one of the paths that I have

``


> match p=(a:RM)-[r1:transport]->(b:RM)-[r2:transport]->(c:RM)-[r3:transport]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r6:transport]->(g:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)

Can I get the position of the nodes like a.tier=1, b.tier=2, c.tier=3, d.tier=4, e.tier=5, f.tier=6, g.tier=7, h.tier=8, i.tier=9, j.tier=10, k.tier=11

Is it possible through apoc or in any other way in which I can get the position number of the nodes.

Thanks,
Pragya
`

Hello @pragyasood28 :slight_smile:

For each path, you will have a list of dictionaries composed with the node and its tier:

MATCH p=(a:RM)-[r1:transport]->(b:RM)-[r2:transport]->(c:RM)-[r3:transport]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r6:transport]->(g:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)
WITH nodes(p) AS nodes
RETURN [i in range(0, size(nodes)-1) | {n: nodes[i], tier: i}]

Regards,
Cobra

1 Like

Hi @Cobra ,

Thanks for the reply, this brings me close to the solution.
Basically I want to set tier 1,2,3...n as a property of every node like (a:RM) should have property a.tier=1 , (b:RM) should have b.tier=2, and so on till end node.

Thanks,
Pragya :slight_smile:

It will set tier property starting from 1 (remove +1 if you want to start from 0):

MATCH p=(a:RM)-[r1:transport]->(b:RM)-[r2:transport]->(c:RM)-[r3:transport]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r6:transport]->(g:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)
WITH nodes(p) AS nodes
UNWIND range(0, size(nodes)-1) AS tier
MATCH (n)
WHERE id(n) = id(nodes[tier])
SET n.tier = tier + 1

Be aware that if you have paths with common nodes, the property will be overwritten.

1 Like

Thanks a lot @Cobra. This is really helpful exactly what I needed.
And yes I am aware if paths are there with common nodes then the property will be overwritten, I have handled that by setting tier=0 for all nodes in start and then setting the tier property only if tier=0, otherwise it remains same.


MATCH p=(a:RM)-[r1:transport]->(b:RM)-[r2:transport]->(c:RM)-[r3:transport]->(d:WIPProduction)-[r4:transport]->(e:WIPStorage)-[r5:transport]->(f:WIPStorage)-[r6:transport]->(g:WIPStorage)-[r7:transport]->(h:Production)-[r8:transport]->(i:Storage)-[r9:transport]->(j:Storage)-[r10:transport]->(k:Storage)
WITH nodes(p) AS nodes
UNWIND range(0, size(nodes)-1) AS tier
MATCH (n)
WHERE id(n) = id(nodes[tier])
SET n.tier = (case when n.tier=0 then tier + 1 else n.tier end)
1 Like