可选匹配问题

时间:2015-06-04 08:20:45

标签: neo4j cypher

我对如何正确使用OPTIONAL MATCH很困惑。使用前面报道的相同例子(enter link description here),我想从mol25开始遍历其支架并返回任选地连接其他分子。

MATCH path=(:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) 
OPTIONAL MATCH (s) <-[:hasSubstructure]- (:Molecule) 
WHERE s.Num_Rings > 1 
RETURN path

enter image description here

虽然支架-1796602943(左上)是mol26的亚结构,但是mol26不返回。我本来期望OPTIONAL MATCH(s)&lt; - [:hasSubstructure] - (:Molecule)返回它。

当在OPTIONAL MATCH之前使用WITH时,我得到所有分子都带有任何支架s,但支架层次结构仅限于第一个邻居,这是预期的行为。

MATCH (:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s:Scaffold) 
WITH s 
OPTIONAL MATCH p=(s) <-[:hasSubstructure]- (:Molecule) 
RETURN p 

那么,我怎样才能在三个关系距离处得到支架,并且在一条路径中可选择地将这些分子连接到任何一个(后续治疗所需)?

enter image description here

2 个答案:

答案 0 :(得分:1)

我想如果你想要返回另一个分子,你实际上必须RETURN它:

MATCH path=(:Molecule {Name: 'mol25'})-[:hasSubstructure*1..3]-(s:Scaffold) 
WHERE s.Num_Rings > 1
OPTIONAL MATCH (s)<-[:hasSubstructure]-(other:Molecule) 
RETURN path, other

答案 1 :(得分:0)

根据上述建议,让我们添加一个名称并返回附加的分子。

MATCH (s:Scaffold) WHERE s.Num_Rings > 1 WITH s
MATCH p=((:Molecule {Name: 'mol25'}) -[:hasSubstructure*1..3]- (s)) 
OPTIONAL MATCH (s) <-[:hasSubstructure]- (m:Molecule)
WHERE s.Num_Rings > 1 
RETURN p,m

它仍然没有返回预期的图形,正确地返回分子,也返回了添加支架,但仍然遍历了Num_Rings = 1的支架。

enter image description here

相关问题