Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-11-2021 02:25 AM
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
`
Solved! Go to Solution.
11-11-2021 05:17 AM
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.
11-11-2021 03:05 AM
Hello @pragyasood28
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
11-11-2021 03:32 AM
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
11-11-2021 05:17 AM
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.
11-11-2021 06:29 AM
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)
All the sessions of the conference are now available online