[英]Nodes with same relation to a third node in a graph database
I was following the Neo4J online tutorial and I came to a question while trying this query with the query tool: 我正在关注Neo4J在线教程,并在使用查询工具尝试此查询时遇到一个问题:
match (a:Person)-[:ACTED_IN|:DIRECTED]->()<-[:ACTED_IN|:DIRECTED]-(b:Person)
return a,b;
I was expecting one of the pairs returned to have the same Person in both identifiers but that didn't happen. 我期望返回的两个对中的一个在两个标识符中都具有相同的Person,但那没有发生。 Can somebody explain me why? 有人可以解释一下为什么吗? Does a match clause exclude repeated elements in the different identifiers used? match子句是否在使用的不同标识符中排除重复的元素?
UPDATE: This question came to me in "Lession 3 - Adding Relationships with Cypher, more" from Neo4J online tutorial, where the query I mentioned above is presented. 更新:这个问题在Neo4J在线教程的“第3课-添加与Cypher的关系,更多”中出现,我在上面提出了我提到的查询。 I refined the query to the following one, in order to focus more directly my question: 我将查询优化为以下查询,以便更直接地关注我的问题:
MATCH (a:Person {name:"Keanu Reeves"})-[:ACTED_IN]->()<-[:ACTED_IN]-(b)
RETURN a,b;
The results: 结果:
|---------------|--------------------|
| a | b |
|---------------|--------------------|
| Keanu Reeves | Carrie-Anne Moss |
| Keanu Reeves | Laurence Fishburne |
| Keanu Reeves | Hugo Weaving |
| Keanu Reeves | Brooke Langton |
| Keanu Reeves | Gene Hackman |
| Keanu Reeves | Orlando Jones |
|------------------------------------|
So, why there is no row with Keanu Reeves in a and b? 那么,为什么在a和b中没有与Keanu Reeves发生争执? Doesn't he should match with both both relations :ACTED_IN? 他不应该同时满足两种关系:ACTED_IN吗?
The behavior you observed is by design. 您观察到的行为是设计使然。
To quote the manual : 引用手册 :
While pattern matching, Cypher makes sure to not include matches where the same graph relationship is found multiple times in a single pattern. 在进行模式匹配时,Cypher确保不包含在单个模式中多次发现相同图形关系的匹配。 In most use cases, this is a sensible thing to do. 在大多数使用情况下,这是明智的做法。
I would check your data sample. 我会检查您的数据样本。 Your query looks like it works just fine for me. 您的查询看起来对我来说很好。 I replicated with a simple data set, and here's verification that it does produce pairs like what you're looking for. 我用一个简单的数据集进行了复制,这里的验证是它确实会产生与您想要的对类似的对。
Joe acted in "Some Flick" 乔饰演《 Some Flick》
neo4j-sh (?)$ create (p:Person {name:"Joe"})-[:ACTED_IN]->(m:Movie {name:"Some Flick"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Relationships created: 1
Properties set: 2
Labels added: 2
14 ms
But Joe is so multi-talented, he also directed "Some Flick". 但是乔是如此多才多艺,他还执导过《 Some Flick》。
neo4j-sh (?)$ match (p:Person {name: "Joe"}), (m:Movie {name: "Some Flick"}) create p-[:DIRECTED]->m;
+-------------------+
| No data returned. |
+-------------------+
Relationships created: 2
23 ms
So who are the actor/director pairs that we know of? 那么,我们认识的演员/导演对是谁?
neo4j-sh (?)$ match (a:Person)-[:ACTED_IN|:DIRECTED]->()<-[:ACTED_IN|:DIRECTED]-(b:Person)
> return a,b;
+-----------------------------------------------------+
| a | b |
+-----------------------------------------------------+
| Node[222128]{name:"Joe"} | Node[222128]{name:"Joe"} |
| Node[222128]{name:"Joe"} | Node[222128]{name:"Joe"} |
+-----------------------------------------------------+
2 rows
50 ms
Of course it's Joe. 当然是乔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.