Cypher查询确保所有节点都在路径中但其他路径可以存在

时间:2017-01-24 19:05:55

标签: neo4j cypher

我有一个图表,但需要确保所有节点都在路径中(但是路径中可以存在多个节点)。

这是一个例子(对不起,不得不把一些东西弄掉):

我希望在我传入的列表中的所有三个中间节点中具有相同属性的值时找到end2而不是end1。但是我无法获得在没有end1的情况下将返回end2的查询。可以有更多的节点具有相同的路由,但我只会传递不同的值,这些值不会跨中间节点重复。任何人都知道一个查询只给我一个具有中间节点所有值的终端节点?还有一些节点挂在那些端节点上,其中一些节点在end1和end2之间相互链接。其他一些没有,那些是我不想要的节点,但因为黄色和蓝色之间的路径到那个端1我不能使用任何但是因为有那些相同节点的其他路径(没有图示)我不能使用ALL。

提前感谢您的帮助。

[更新] 这是我使用的当前查询,但它只允许每个起始节点有一个“结束”节点,我想要多个。我需要传入的id(例如)= {eg_id}但是将其限制为1。我宁愿使用以下事实:下面的路径中的每个a都需要匹配中间节点中的名称属性列表,必须在那里才能到达哪个端节点。因此,如果黄色和蓝色是htere然后end1和end2会回来但是如果有黄色,蓝色和紫色那么只有end2会回来。

start td = node({td_id}) 
match (td:Start)-[:Rel1]->(a)<-[:Rel2]-(eg:End)-[es:Rel3]->(n:WhatsPastEnd) 
with collect(a.name) as pnl, n, td, eg, es 
where id(eg) = {eg_id} 
and all(param_needs in {param_name_list} where param_needs in pnl) 
return n 
order by es.order

[解决]

非常感谢InverseFalcon,从下面的解决方案中得到了我需要的东西!

1 个答案:

答案 0 :(得分:0)

好的,让我们修改你的查询,并删除endnode id的匹配。

start td = node({td_id}) 
// unwind your list of names so every row has a name
with td, {param_name_list} as param_names
unwind param_names as param_name
match (td:Start)-[:Rel1]->(a)
where a.name = param_name
// now a has all nodes with the required names
// collect then unwind so we have the full collection for each a
with collect(a) as requiredNodes
unwind requiredNodes as a
match (a)<-[:Rel2]-(eg:End)
where all(node in requiredNodes where (node)<-[:Rel2]-(eg))
with eg
match (eg)-[es:Rel3]->(n:WhatsPastEnd) 
return n 
order by es.order