简体   繁体   English

Neo4j / Cypher:计算与模式匹配的不同节点组合

[英]Neo4j/Cypher: Count distinct node combinations that match a pattern

I query paths within a Neo4j graph. 我查询Neo4j图中的路径。 The path contains more than two nodes. 该路径包含两个以上的节点。 I want to count the distinct occurences of a two-node subpath. 我想计算两个节点子路径的不同出现次数。

So in the following example I want to know the number of the resulting rows: 因此,在以下示例中,我想知道结果行的数量:

MATCH ()-->(n1:Label1)-->(n2:Label2)-->()
RETURN DISTINCT n1, n2

Eg something like 例如类似

RETURN count(DISTINCT n1, n2)

(which would work for a single node: RETURN count(DISTINCT n1) ) (适用于单个节点: RETURN count(DISTINCT n1)

How can I do this in Cypher? 如何在Cypher中做到这一点?

Do this to get the number of times each distinct n1 and n2 combination is found: 这样做以获得找到每个不同的n1n2组合的次数:

MATCH ()-->(n1:Label1)-->(n2:Label2)-->()
RETURN n1, n2, COUNT(*);

Aggregating functions like COUNT use non-aggregating items in the same WITH or RETURN clause as unique "grouping keys" (so no need to use DISTINCT ). 诸如COUNT类的聚合函数将同一WITHRETURN子句中的非聚合项用作唯一的“分组键”(因此无需使用DISTINCT )。

[UPDATE] [UPDATE]

To get the number of distinct n1 and n2 combinations, you can aggregate twice in this somewhat hacky query: 要获得不同的n1n2组合的数量,您可以在这个有点古怪的查询中进行两次汇总:

MATCH ()-->(n1:Label1)-->(n2:Label2)-->()
WITH n1, n2, COUNT(*) AS ignored
RETURN COUNT(*) AS nCombos;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM