简体   繁体   English

neo4j:Cypher查询中的可选“步骤”

[英]neo4j: optional 'steps' in Cypher query

I am trying to find relations between nodes with optional but specific nodes/relationships in between (neo4j 2.0 M6). 我试图找到节点之间的关系,其间有可选但特定的节点/关系(neo4j 2.0 M6)。

In my data model, 'Gene' can be 'PARTOF' a 'Group. 在我的数据模型中,'Gene'可以是'PARTOF'''组。 I have 'INTERACT' relationships between 'Gene'-'Gene', 'Gene'-'Group' and 'Group'-'Group' (red lines in model image). 我在“基因” - “基因”,“基因” - “群组”和“群组” - “群组”(模型图像中的红线)之间存在“互动”关系。

I want to boil this down to all 'INTERACT' relationships between 'Gene': both direct (Gene-INTERACT-Gene) and via one or two 'Group' (Gene-PARTOF-Group-INTERACT-Gene) . 我想把它归结为'基因'之间的所有'交互'关系:直接(Gene-INTERACT-Gene)和通过一个或两个'组' (Gene-PARTOF-Group-INTERACT-Gene)


在此输入图像描述


Of course this is easy with multiple Cypher queries: 当然,对于多个Cypher查询,这很容易:

# direct INTERACT
MATCH (g1:Gene)-[r:INTERACT]-(g2:Gene) RETURN g1, g2
# INTERACT via one Group
MATCH (g1:Gene)-[:PARTOF]-(gr:Group)-[r:INTERACT]-(g2:Gene) RETURN g1, g2
# INTERACT via two Group
MATCH (g1:Gene)-[:PARTOF]-(gr1:Group)-[r:INTERACT]-(gr2:Group)-[:PARTOF]-(g2:Gene)
RETURN g1, g2

But would it be possible to construct a single Cypher query that takes optional 'Group steps' in the path? 但是,是否可以构建一个在路径中采用可选“组步骤”的Cypher查询? So far I only used optional relationships and shortestPaths , but I have no idea if I can filter for one or two optional nodes in between two genes. 到目前为止,我只使用了可选关系和shortestPaths ,但我不知道是否可以在两个基因之间过滤一个或两个可选节点。

You can assign a depth between zero and one for each of the relationships you add to the path. 您可以为添加到路径的每个关系指定0到1之间的深度。 Try something like 尝试类似的东西

MATCH (g1:Gene)-[:PARTOF*0..1]-(gr1)-[:INTERACT]-(gr2)-[:PARTOF*0..1]-(g2:Gene) 
RETURN g1,g2

and to see what the matched paths actually look like, just return the whole path 并且要查看匹配的路径实际上是什么样的,只需返回整个路径

MATCH p=(g1:Gene)-[:PARTOF*0..1]-(gr1)-[:INTERACT]-(gr2)-[:PARTOF*0..1]-(g2:Gene) 
RETURN p

There is a bit of a bother with declaring node labels for the optional parts of this pattern, however, so this query assumes that genes are not part of anything other than groups, and that they only interact with groups and other genes. 然而,对于此模式的可选部分声明节点标签有点麻烦,因此该查询假定基因不是除了组之外的任何其他部分,并且它们仅与组和其他基因相互作用。 If a gene can have [:PARTOF] to something else, then (gr1) will bind that something, and the query is no longer reliable. 如果一个基因可以将[:PARTOF]在其他东西中,那么(gr1)将绑定该东西,并且查询不再可靠。 Simply adding a where clause like 只需添加一个where子句即可

WHERE gr1:Group AND gr2:Group

excludes the case where the optional parts are not matched, so that won't work (it'd be like your third query). 排除了可选部分不匹配的情况,因此不起作用(它就像你的第三个查询)。 I'm sure it can be solved but if your actual model is not much more complex than what you describe in your question, this should. 我确信它可以解决,但如果你的实际模型并不比你在问题中描述的那么复杂,那么这应该是。

I took the liberty of interpreting your model in a console here , check it out to see if it does what you want. 我参加了一个控制台解释模型的自由在这里 ,检查它,看它是否你想要做什么。

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

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