在后续查询中使用可选的密码查询结果并合并结果

问题描述

我有一个有效的密码查询获取可选结果,然后在后续查询中使用结果节点并组合结果。

MATCH (n:KNowledgeEntry {id: 'd0634a24-91d0-4fd7-8868-2caa3ab0dc7a' })
OPTIONAL MATCH (n)-[r:IS_PART]->(p:KNowledgeEntry)
OPTIONAL MATCH (n)<-[ch:IS_PART*1..2]-(d:HierarchicalKNowledgeEntry)
UNWIND d AS descendant
UNWIND p AS parent
UNWIND r AS parentRelation
UNWIND ch AS childRelation
WITH (collect(disTINCT n) + collect(disTINCT parent) + collect(disTINCT descendant)) as  nodes,parentRelation,childRelation
UNWIND nodes AS n
OPTIONAL MATCH (n)-[r:CONTEXTUAL_KNowLEDGE]-(c:ContextualKNowledgeEntry)
UNWIND nodes AS node
UNWIND c AS contextualNode
UNWIND r AS contextualRelation
WITH collect(disTINCT node) + collect(disTINCT contextualNode) as kNowledgeEntries,collect(distinct parentRelation) + collect(distinct childRelation) + collect(distinct contextualRelation) as edges
return kNowledgeEntries,edges

这有点难看,我在问自己是否有更好的方法来构建此查询

有什么想法吗?

解决方法

正如 InverseFalcon 在评论部分所说的,UNWIND 就像一个列表的“for 循环”。您可以按如下方式重新编写查询。

my_matrix[0][0]:[my_matrix[1][0],my_matrix[0][1]]