简体   繁体   English

Neo4J Cypher: Match ()-[]-() Return count(*) 未分配节点标签时返回关系数的两倍

[英]Neo4J Cypher: Match ()-[]-() Return count(*) return double of the number of the relationships when node labels are not assigned

In my database, there is a kind of relationships between A and B named awithb .在我的数据库中, AB之间存在一种名为awithb的关系。 For the Cypher对于 Cypher

MATCH ()-[:awithb]-() return count(*)

it returns 140.它返回 140。

However if we give the labels of the node,但是,如果我们给出节点的标签,

MATCH (:A)-[:awithb]-(:B) return count(*)

it returns 70.它返回 70。

Why these two cypher queries return different results?为什么这两个 cypher 查询返回不同的结果?

Cypher is about returning paths that match patterns. Cypher 是关于返回匹配模式的路径。

Paths are ordered sequences of nodes and relationships, and when no direction is specified, and no labels are specified, then a pattern like that will produce two paths using the same relationship and the same two nodes, just the order of the nodes in the path is different, and you traverse the relationship in the other direction.路径是节点和关系的有序序列,当没有指定方向,也没有指定标签时,这样的模式将使用相同的关系和相同的两个节点产生两条路径,只是路径中节点的顺序是不同的,你从另一个方向遍历关系。

If you add a direction to the pattern, then only one path is possible:如果向模式添加方向,则只有一条路径是可能的:

MATCH ()-[:awithb]->() 
RETURN count(*)

When you put no labels on your query, it means that you want ANY nodes that have that relationship.当您在查询中不放置任何标签时,这意味着您需要具有该关系的任何节点。

 () - [: awithb] - ()  where () is any nodes 

When you put non-directional path with corresponding labels;当您放置带有相应标签的非定向路径时; it means the relationship can be from A to B or B to A.这意味着关系可以是从 A 到 B 或 B 到 A。

 (:A) - [: awithb] - (:B) equals  A->B  OR  A<-B

Thus, in your first query, you are getting 2x the counts because you are getting the path from A to B and B to A.因此,在您的第一个查询中,您获得了 2 倍的计数,因为您获得了从 A 到 B 和 B 到 A 的路径。

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

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