Currently I have a query that returns the correct amount of child nodes, but the number of items is only displayed for each node, not summed up in total. Not sure if I'm missing anything here, or if this is not the right approach?
It's important to note that I can't rely on specifying the starting node by any property myself, as it can change over time inside the db, therefore the query should start with the top Category node that does not have a parent.
MATCH p = (c:Category)-[:IS_PARENT_OF *0..]->(c)
WITH c, apoc.text.join("1" + [rel in relationships(p) | rel.index], '.') as path, size((:Category)<-[:IS_PARENT_OF*]-(c)) as childCount, size((:Item)-[:IS_CATEGORIZED_AS]->(c)) as itemCount, c.name AS name
ORDER BY path
RETURN name, childCount, itemCount
Next, I wrote this Cypher to count like this.
I'm sure there are better Cypher, but I wrote it without using APOC.
MATCH (category:Category)
OPTIONAL MATCH (category)-[:IS_PARENT_OF*..10]->(c)
OPTIONAL MATCH (category)<-[:IS_CATEGORIZED_AS]-(item1:Item)
OPTIONAL MATCH (c)<-[:IS_CATEGORIZED_AS]-(item2:Item)
RETURN category.name AS category,
count(DISTINCT(c)) AS childCount,
count(DISTINCT(item1)) + count(DISTINCT(item2)) AS itemCount
This Cypher with a label like this would be better.
Changed the 2nd line from "->(c)" to "->(c:Category)".
MATCH (category:Category)
OPTIONAL MATCH (category)-[:IS_PARENT_OF*..10]->(c:Category)
OPTIONAL MATCH (category)<-[:IS_CATEGORIZED_AS]-(item1:Item)
OPTIONAL MATCH (c)<-[:IS_CATEGORIZED_AS]-(item2:Item)
RETURN category.name AS category,
count(DISTINCT(c)) AS childCount,
count(DISTINCT(item1)) + count(DISTINCT(item2)) AS itemCount