Cypher:从节点列表中获取相关节点的列表

时间:2018-07-04 12:18:55

标签: list neo4j cypher

我有一个类似的模型:

enter image description here (变电站为蓝色,装置为黄色)

我匹配从某些变电站到某些变电站的路径。

Match (s1:Substation) - [] - (:installation) - [ :feeds1..40 ] - (:installation) - (s2:Substation)

这将产生一个起始变电站,1个或更多安装,然后一个封闭变电站的路径(当然还有之间的许多关联) 但是在输出中,我只想查看所有连接的变电站。 如果只有安装路径在我的路径上,我该怎么办?

我尝试过类似的东西:

with p, filter ( x IN nodes(p) where "ms_installatie" in LABELS(x) ) as installaties,

但是这里没有好的答案。

最后,我希望所有变电站的CSV输出“在”路径上(而不是“在”内,那很容易!)

欢迎提出任何建议。

建议查询的简化输出:

[[
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV""nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV","nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV","nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"}
]]

实际上是3条路径(返回路径(p)时显示,请参阅enter image description here)。我想要的输出:

[[
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV""nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"}
][
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV","nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"}
][
{"naam":"TILBURG NOORD","nummer":"TBN"},
{"naam":"QUIRIJNSTOK","nummer":"152.6870"},
{"naam":"BP NOORD-OOST TANGEND","nummer":"152.6258"},
{"naam":"VERELST BV","nummer":"152.2020"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"},
{"naam":"L.O.A. A1","nummer":"152.1200A1"}
]]

最后一个站点的加倍/三倍是正确的,因为该站点有3个设施,彼此相互喂食。

谢谢。

1 个答案:

答案 0 :(得分:0)

我不确定此查询是否100%可靠(我无法在您的数据上对其进行测试),但它应该非常接近:

MATCH p=(s1:Substation)-->(:installation)-[:feeds*..40 ]-(:installation)<--(s2:Substation)
WITH s1 AS start, s2 AS end, filter(x IN NODES(p) WHERE "ms_installatie" in LABELS(x) ) as installaties
 UNWIND installaties as node
   MATCH (node)<--(ss:Substation)
   WITH start, end, collect(ss) AS ss_on_path
   RETURN [] + start + ss_on_path + end