Neo4j与两个关系分组 [英] Neo4j grouping with two relationships
问题描述
我有一个图表,表示这个节点的层次结构。
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.
假设我正在为节点A请求数据。我希望以最近的组关系对节点进行重组。我会得到这样的东西:
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.
这是我的neo4js db:
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);
推荐答案
如果您想查看任何给定的组织数group,这很简单:
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.
这篇关于Neo4j与两个关系分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!