Neo4j grouping with two relationships

2019-08-02 12:18发布

I have a chart that will represent hierarchy for nodes like this one. expended graph

Lets say that this one is representing the complete hierarchy but I also need to create intermediate result by grouping at different levels.

Suppose I'm requesting data for node A. I wish to regroup nodes at the nearest Group relationship. I'd to get something like this: graph summary A

Basically users will be allowed to associate nodes to group and I need to represent the data in the convenient way to display an Org Chart.

I don't know where to start to get the optimal solution.

Here's my neo4js db:

CREATE (a:Node { name: 'a' }), (b:Node { name: 'b' }), 
   (c:Node { name: 'c' }), (d:Node { name: 'd' }),
   (e:Node { name: 'e' }), (f:Node { name: 'f' }), 
   (g:Node { name: 'g' }), (h:Node { name: 'h' }),

   (g1:Group { name: 'group1'}), 
   (g2:Group { name: 'group2'}),
   (g3:Group { name: 'group3'}),

   (a)-[:child]->(b), 
   (a)-[:child]->(c),
   (a)-[:child]->(d),
   (b)-[:child]->(e),
   (c)-[:child]->(f),
   (c)-[:child]->(g),
   (c)-[:child]->(h),

   (b)-[:belongsTo]->(g1),
   (c)-[:belongsTo]->(g2),
   (g)-[:belongsTo]->(g3),
   (h)-[:belongsTo]->(g3);

Neo4j console

1条回答
唯我独甜
2楼-- · 2019-08-02 13:03

If you want to see how many organizations are contained in any given group, this is straightforward:

MATCH (g:Group)<-[:belongsTo]-(x:Node)
RETURN g.name, count(x);

That will give you each group name, along with how many nodes are in it.

查看更多
登录 后发表回答