Cypher查询中的逗号有什么作用? [英] What does a comma in a Cypher query do?

查看:72
本文介绍了Cypher查询中的逗号有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个同事编码如下:

match (a)-[r]->(b), (c) set c.x=y

逗号是做什么用的?只是MATCH的简写吗?

What does the comma do? Is it just shorthand for MATCH?

推荐答案

由于Cypher的ASCII语法只能使您连续指定一个线性连接链,因此逗号(至少部分地)可以使您指定可能会分支的事物.例如:

Since Cypher's ASCII-art syntax can only let you specify one linear chain of connections in a row, the comma is there, at least in part, to allow you to specify things that might branch off. For example:

MATCH (a)-->(b)<--(c), (b)-->(d)

代表三个都连接到b的节点(两个传入关系和一个传出关系.

That represents three nodes which are all connected to b (two incoming relationships, and one outgoing relationship.

如果您的匹配太长,逗号也可以用于分隔行,如下所示:

The comma can also be useful for separating lines if your match gets too long, like so:

MATCH
  (a)-->(b)<--(c),
  (c)-->(d)

很明显,这不是很长的一行,但它等效于:

Obviously that's not a very long line, but that's equivalent to:

MATCH
  (a)-->(b)<--(c)-->(d)

但是,通常,任何MATCH语句都指定要搜索的模式.该MATCH的所有部分均构成该模式.在您的情况下,您实际上正在寻找两个未连接的模式((a)-[r]->(b)(c)),因此Neo4j将找到两种模式的每个实例的每种组合,这可能会非常昂贵.在Neo4j 2.3中,您可能还会收到有关此查询的警告,该查询会给您笛卡尔积.

But in general, any MATCH statement is specifying a pattern that you want to search for. All of the parts of that MATCH form the pattern. In your case you're actually looking for two unconnected patterns ((a)-[r]->(b) and (c)) and so Neo4j will find every combination of each instance of both patterns, which could potentially be very expensive. In Neo4j 2.3 you'd also probably get a warning about this being a query which would give you a cartesian product.

但是,如果指定多个匹配项,则要求搜索不同的模式.因此,如果您这样做:

If you specify multiple matches, however, you're asking to search for different patterns. So if you did:

MATCH (a)-[r]->(b)
MATCH (c)

从概念上讲,我认为这有点不同,但是结果是相同的.我知道与OPTIONAL MATCH绝对不同.如果您这样做:

Conceptually I think it's a bit different, but the result is the same. I know it's definitely different with OPTIONAL MATCH, though. If you did:

MATCH (a:Foo)
OPTIONAL MATCH (a)-->(b:Bar), (a)-->(c:Baz)

您只会找到其中Foo节点未连接或同时连接到BarBaz节点的实例.而如果您这样做:

You would only find instances where there is a Foo node connected to nothing, or connected to both a Bar and a Baz node. Whereas if you do this:

MATCH (a:Foo)
OPTIONAL MATCH (a)-->(b:Bar)
OPTIONAL MATCH (a)-->(c:Baz)

您将找到每个Foo节点,并分别匹配零个或多个连接的BarBaz节点.

You'll find every single Foo node, and you'll match zero or more connected Bar and Baz nodes independently.

在评论中,Stefan Armbruster指出,逗号也可用于将子模式分配给各个标识符.如:

In the comments Stefan Armbruster made a good point that commas can also be used to assign subpatterns to individual identifiers. Such as in:

MATCH path1=(a)-[:REL1]->(b), path2=(b)<-[:REL2*..10]-(c)

谢谢斯蒂芬!

EDIT2 :另请参见下面的Mats答案

Also see Mats' answer below

这篇关于Cypher查询中的逗号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆