Cypher查询:获取按关系属性

时间:2018-01-09 19:29:20

标签: neo4j cypher

我是Neo4j / Cypher的新手,我在关系属性方面遇到了一些麻烦。

首先是玩具示例:

CREATE (A1:Worker {ref:"A1"})
CREATE (A2:Worker {ref:"A2"})

CREATE (B1:Worker {ref:"B1"})
CREATE (B2:Worker {ref:"B2"})

CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B2)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B2)

CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B2)

这将创建一个包含4个工作节点的图形,其中A节点通过关系连接到B节点,这些关系可以具有不同的"类型"属性。在这种情况下,A1通过2种不同类型的流连接到B,而A2仅通过1种类型连接:

sample graph

我希望能够做的是计算每个源节点的传出关系的数量,但是按照" type"的各种值对它们进行分组。关系中的财产得到这样的东西:

+--------+-------------+---------------+
| Worker | StreamType  | OutgoingCount |
+--------+-------------+---------------+
| A1     | stream1     | 2             |
+--------+-------------+---------------+
| A1     | stream2     | 2             |
+--------+-------------+---------------+
| A2     | stream1     | 2             |
+--------+-------------+---------------+

到目前为止,我可以获得总输出和不同输出类型的数量:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker)
RETURN source.ref as Source, 
       COUNT(st) as TotalOutgoing, 
       COUNT(distinct st.type) as NumberOfTypes;

任何提示都会有所帮助。

1 个答案:

答案 0 :(得分:2)

所以事实证明这是微不足道的!我没有理解你和COUNT()函数一起返回的内容是:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker) 
RETURN source.ref as Worker, 
       st.type as StreamType, 
       COUNT(st) as OutgoingCount;