My graph consists Nodes that have CHILD_OF relationships. I'm able to query and generate a tree structure with the following query:
MATCH p = (:Node { name:"Root" })<-[:CHILD_OF *0..]-(c:Node) with *, relationships(p) as i
with REDUCE (path = '1', index IN i | path + '.' + index.index) AS path, c.name as name
ORDER BY path
RETURN { path: path, name: name }
The query returns:
╒════════════════════════════════════╕
│"{ path: path, name: name }" │
╞════════════════════════════════════╡
│{"path":"1","name":"Root"} │
├────────────────────────────────────┤
│{"path":"1.1","name":"Sub Assy 1"} │
├────────────────────────────────────┤
│{"path":"1.1.1","name":"Part 1"} │
├────────────────────────────────────┤
│{"path":"1.1.2","name":"Part 2"} │
├────────────────────────────────────┤
│{"path":"1.2","name":"Sub Assy 2"} │
├────────────────────────────────────┤
│{"path":"1.2.1","name":"Sub Assy 1"}│
├────────────────────────────────────┤
│{"path":"1.2.1.1","name":"Part 1"} │
├────────────────────────────────────┤
│{"path":"1.2.1.2","name":"Part 2"} │
├────────────────────────────────────┤
│{"path":"1.3","name":"Sub Assy 3"} │
└────────────────────────────────────┘
In order to create a new child, I need to determine the number of existing children for the parent node first. When I try to count them I always get a result of one.
MATCH p = (:Node { name:"Root" })<-[:CHILD_OF *0..]-(c:Node) with *, relationships(p) as i
with COUNT ((:Node { ID: c.ID })<-[:CHILD_OF]-(:Node)) as childcount, i, c
with *, REDUCE (path = '1', index IN i | path + '.' + index.index) AS path, c.name as name
ORDER BY path
RETURN { path: path, name: name, childcount: childcount }
Which returns:
╒════════════════════════════════════════════════════╕
│"{ path: path, name: name, childcount: childcount }"│
╞════════════════════════════════════════════════════╡
│{"path":"1","name":"Root","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.1","name":"Sub Assy 1","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.1.1","name":"Part 1","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.1.2","name":"Part 2","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.2","name":"Sub Assy 2","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.2.1","name":"Sub Assy 1","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.2.1.1","name":"Part 1","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.2.1.2","name":"Part 2","childcount":1} │
├────────────────────────────────────────────────────┤
│{"path":"1.3","name":"Sub Assy 3","childcount":1} │
└────────────────────────────────────────────────────┘
Which is incorrect. "Root" should have 3 children and "Sub Assy 1" should have 2.
If I do the count a count for "Root" and "Sub Assy 1" all by themselves I get the correct results.
MATCH (:Node { name:"Root" })<-[i:CHILD_OF]-(c:Node) return count(i)
Correctly returns 3
MATCH (:Node { name:"Sub Assy 1" })<-[i:CHILD_OF]-(c:Node) return count(i)
Correctly returns 2
How do I get the correct childcount in my recursive query?