我有一个查询,该查询找到2个节点之间的最短路径。这些节点被标记为设备,但它们也连接到Apps节点。因此,我有2个app_id,我希望通过这些ID查找与这些app_id相关的2个设备之间的所有连接,以及这些连接中所有受影响设备的信息。所以我有这个查询:
WITH ["000001", "000002"] as IDS
UNWIND IDS as app_ids
MATCH (d:Device)-[:DEV_APP]->(a:Apps {app_id: app_ids})
WITH COLLECT(d) as device, COLLECT(distinct a) as app
WITH device[1] as first, device[7] as second, app
WITH first.device_id AS id1, second.device_id as id2, application
MATCH path=allShortestPaths((d1:Device {device_id:id1})-[:DEV_COL1|DEV_COL2|:DEV_COL3*..5]->(d2:Device {device_id:id2}))
RETURN path, application
此查询返回节点之间的所有路径,但是,我还需要有关所有连接到受影响设备的应用程序的数据。结果是,我仅获得所有“设备”节点数据以及2个“ Apps”节点数据。我知道发生这种情况是因为我在第一个Match查询中过滤了Apps节点。因此,我尝试执行以下操作以在查询中查找与受影响的设备相关的所有Apps数据:
WITH ["000001", "000002"] as IDS
UNWIND IDS as app_ids
MATCH (d:Device)-[:DEV_APP]->(a:Apps {app_id: app_ids})
WITH COLLECT(d) as device, COLLECT(distinct a) as app, COLLECT(distinct d.device_id) as device_ids
MATCH (d_all:Device {device_id: device_ids})-[:DEV_APP]->(a_all:Apps)
WITH device[1] as first, device[7] as second, app, COLLECT(a_all) as apps
WITH first.device_id AS id1, second.device_id as id2, application, apps
MATCH path=allShortestPaths((d1:Device {device_id:id1})-[:DEV_COL1|DEV_COL2|:DEV_COL3*..5]->(d2:Device {device_id:id2}))
RETURN path, application, apps
问题是结果没有记录。虽然,我检查了节点的device_ids列表是否为空。当我分别运行此查询的各个部分时,结果将正常显示。此查询可能是什么错误?
答案 0 :(得分:1)
问题出在第四行:
MATCH (d_all:Device {device_id: device_ids})-[:DEV_APP]->(a_all:Apps)
尤其是device_ids
。这是上一行的一个集合,但是我想这里没有:Device,其中device_id是一个列表,其中所有这些列表元素均以该特定顺序排列。
我的猜测是,您打算与列表中具有设备ID的设备进行匹配,而使用这种方法无法做到这一点。
这是您想要做的一种方法:
...
MATCH (d_all:Device)-[:DEV_APP]->(a_all:Apps)
WHERE d_all.device_id IN device_ids
...
或替代:
...
UNWIND device_ids as device_id
MATCH (d_all:Device {device_id:device_id)-[:DEV_APP]->(a_all:Apps)
...
或者如果设备集合中包含您想要的所有设备:
...
// using plurals for variable names of lists
WITH COLLECT(d) as devices, COLLECT(distinct a) as apps, COLLECT(distinct d.device_id) as device_ids
UNWIND devices as device
MATCH (device)-[:DEV_APP]->(a_all:Apps)
...