简体   繁体   English

Cypher 查询中的多个 MATCH 子句和逗号有什么区别?

[英]What is the difference between multiple MATCH clauses and a comma in a Cypher query?

In a Cypher query language for Neo4j, what is the difference between one MATCH clause immediately following another like this:在 Neo4j 的 Cypher 查询语言中,一个 MATCH 子句紧跟另一个这样的子句之间有什么区别:

MATCH (d:Document{document_ID:2})
MATCH (d)--(s:Sentence)
RETURN d,s

Versus the comma-separated patterns in the same MATCH clause?与同一 MATCH 子句中的逗号分隔模式相比? Eg:例如:

MATCH (d:Document{document_ID:2}),(d)--(s:Sentence)
RETURN d,s

In this simple example the result is the same.在这个简单的例子中,结果是一样的。 But are there any "gotchas"?但是有什么“陷阱”吗?

There is a difference: comma separated matches are actually considered part of the same pattern.有一个区别:逗号分隔的匹配实际上被认为是同一模式的一部分。 So for instance the guarantee that each relationship appears only once in resulting path is upheld here.因此,例如,这里支持每个关系在结果路径中只出现一次的保证。

Separate MATCHes are separate operations whose paths don't form a single patterns and which don't have these guarantees.单独的 MATCH 是单独的操作,其路径不形成单一模式并且没有这些保证。

I think it's better to explain providing an example when there's a difference.我认为最好在有差异时提供一个例子来解释。 Let's say we have the " Movie " database which is provided by official Neo4j tutorials.假设我们有官方 Neo4j 教程提供的“电影”数据库。 And there're 10 :WROTE relationships in total between :Person and :Movie nodes :Person:Movie节点之间总共有 10 个:WROTE关系

MATCH (:Person)-[r:WROTE]->(:Movie) RETURN count(r); // returns 10

1) Let's try the next query with two MATCH clauses: 1) 让我们用两个 MATCH 子句尝试下一个查询:

MATCH (p:Person)-[:WROTE]->(m:Movie) MATCH (p2:Person)-[:WROTE]->(m2:Movie)
RETURN p.name, m.title, p2.name, m2.title;

Sure you will see 10*10 = 100 records in the result.当然你会在结果中看到 10*10 = 100条记录。

2) Let's try the query with one MATCH clause and two patterns: 2) 让我们尝试使用一个 MATCH 子句和两种模式进行查询:

MATCH (p:Person)-[:WROTE]->(m:Movie), (p2:Person)-[:WROTE]->(m2:Movie) 
RETURN p.name, m.title, p2.name, m2.title;

Now you will see 90 records are returned.现在您将看到返回了90条记录。 That's because in this case records where p = p2 and m = m2 with the same relationship between them (:WROTE) are excluded .这是因为在这种情况下, p = p2m = m2之间具有相同关系(:WROTE) 的记录被排除在外

For example, there IS a record in the first case (two MATCH clauses)比如第一种情况下有一条记录(两个MATCH子句)

p.name m.title p2.name m2.title p.name m.title p2.name m2.title

"Aaron Sorkin" "A Few Good Men" "Aaron Sorkin" "A Few Good Men" 《亚伦·索尔金》《几个好人》《亚伦·索尔金》《几个好人》

while there's NO such a record in the second case (one MATCH, two patterns)而在第二种情况下没有这样的记录(一个匹配,两个模式)

There are no differences between these provided that the clauses are not linked to one another .只要这些条款没有相互联系,它们之间就没有区别。

If you did this:如果你这样做:

MATCH (a:Thing), (b:Thing) RETURN a, b;

That's the same as:这与:

MATCH (a:Thing) MATCH (b:Thing) RETURN a, b;

Because (and only because) a and b are independent.因为(并且仅因为) ab是独立的。 If a and b were linked by a relationship, then the meaning of the query could change.如果ab通过关系链接,则查询的含义可能会改变。

In a more generic way, " The same relationship cannot be returned more than once in the same result record. " [see 1.5.以更通用的方式,“在同一个结果记录中不能多次返回相同的关系。 ” [参见1.5。 Cypher Result Uniqueness in the Cypher manual] Cypher 手册中的 Cypher 结果唯一性]

Both MATCH-after-MATCH, and single MATCH with comma-separated pattern should logically return a Cartesian product. MATCH-after-MATCH 和带有逗号分隔模式的单个 MATCH 应该在逻辑上返回笛卡尔积。 Except, for comma-separated pattern, we must exclude those records for which we already added the relationship(s).除了逗号分隔模式之外,我们必须排除那些我们已经为其添加关系的记录。

In Andy's answer, this is why we excluded repetitions of the same movie in the second case: because the second expression from each single MATCH was using there the same :WROTE relationship as the first expression.在 Andy 的回答中,这就是为什么我们在第二种情况下排除了同一部电影的重复:因为每个 MATCH 中的第二个表达式在那里使用与第一个表达式相同的 :WROTE 关系。

If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts.如果查询的一部分包含多个断开连接的模式,这将在所有这些部分之间构建笛卡尔积。 This may produce a large amount of data and slow down query processing.这可能会产生大量数据并减慢查询处理速度。 While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (a)) .虽然偶尔有意,但通常可以重新制定避免使用此交叉产品的查询,也许通过添加不同部分之间的关​​系或使用 OPTIONAL MATCH (标识符是:(a))。 IN short their is NO Difference in this both query but used it very carefully.简而言之,它们在这两个查询中都没有区别,但非常小心地使用它。

In a more generic way, "The same relationship cannot be returned more than once in the same result record."以更通用的方式,“在同一个结果记录中不能多次返回相同的关系。” [see 1.5. [见1.5。 Cypher Result Uniqueness in the Cypher manual] Cypher 手册中的 Cypher 结果唯一性]

How about this statement?这个说法怎么样?

MATCH p1=(v:player)-[e1]->(n) 
MATCH p2=(n:team)<-[e2]-(m) 
 WHERE e1=e2 
RETURN e1,e2,p1,p2

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

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