获取附加到路径中所有项目的节点,包括顶部元素

Get Nodes attached to all items in a path including the top element

我有以下密码查询

MATCH p=(pInit:part{_id:'XXXXXXXXXX'})-[cons:consumes*]->(items:part) 
OPTIONAL MATCH (items)-[relmop:isMaintainedWithOp]->(mop:maintenanceOps)   
RETURN p,pInit,relmop,mop 
ORDER BY items._sequence

我还想获得附加到 'pInit' 的 'mop' (maintenanceOps),而不必复制“可选匹配”行。 在我的实际示例中,我最多可以有 6 或 7 行,例如可选的匹配行。如果我必须复制它会增加很多行。

在前面的示例中,我想避免的结果是可选的匹配行,它会导致以下查询:

MATCH p=(pInit:part{_id:'XXXXXXXXXX'})-[cons:consumes*]->(items:part) 
OPTIONAL MATCH (items)-[relmop:isMaintainedWithOp]->(mop:maintenanceOps)   
OPTIONAL MATCH (pInit)-[relmop2:isMaintainedWithOp]->(mop2:maintenanceOps)  
RETURN p,pInit,relmop,mop,relmop2,mop2
ORDER BY items._sequence

有什么想法吗?

我会这样做

MATCH p=(pInit:part{_id:'XXXXXXXXXX'})-[cons:consumes*]->(item:part)
UNWIND nodes(p) AS node
WITH p,nodes(p)[0] AS pInit,node,[(node)-[relmop:isMaintainedWithOp]->(:maintenanceOps) | relmop][0] AS relmop
RETURN p,pInit,relmop,endNode(relmop) AS mop,node
ORDER BY node._sequence

这看起来很奇怪,但我这样做解决了我的问题:

MATCH p=(:part{_id:'XXXXXXXXXX'})-[cons:consumes*0..20]->(items:part) 
OPTIONAL MATCH (items)-[relmop:isMaintainedWithOp]->(mop:maintenanceOps)   
RETURN p,pInit,relmop,mop 

允许 0 个连接,似乎使用路径中的第一个对象作为 'items' 并搜索其他连接。