Neo4j grouping with two relationships

僤鯓⒐⒋嵵緔 提交于 2019-12-24 07:58:49

问题


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

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:

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:


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.



来源:https://stackoverflow.com/questions/46136299/neo4j-grouping-with-two-relationships

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!