在后续查询中使用可选的密码查询结果并合并结果
Use optional cypher query results in subsequent query and combine results
我有一个有效的密码查询来获取可选结果,然后在后续查询中使用生成的节点并组合结果。
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 loop'。您可以按如下方式重新编写查询。
MATCH (n:KnowledgeEntry {id: 'd0634a24-91d0-4fd7-8868-2caa3ab0dc7a' })
WITH n
OPTIONAL MATCH (n)-[r:IS_PART]->(p:KnowledgeEntry)
WITH n, p, r
OPTIONAL MATCH (n)<-[ch:IS_PART*1..2]-(d:HierarchicalKnowledgeEntry)
WITH n, p, d, r, ch
WITH (collect(DISTINCT n) + collect(DISTINCT p) + collect(DISTINCT d)) as nodes, collect(DISTINCT r) as parentRelation, collect(DISTINCT ch) as childRelation
UNWIND nodes AS n
OPTIONAL MATCH (n)-[r:CONTEXTUAL_KNOWLEDGE]-(c:ContextualKnowledgeEntry)
WITH n as node, parentRelation, childRelation, c AS contextualNode, r as contextualRelation
WITH collect(DISTINCT node) + collect(DISTINCT contextualNode) as knowledgeEntries, parentRelation + childRelation + collect(distinct contextualRelation) as edges
return knowledgeEntries, edges
我有一个有效的密码查询来获取可选结果,然后在后续查询中使用生成的节点并组合结果。
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 loop'。您可以按如下方式重新编写查询。
MATCH (n:KnowledgeEntry {id: 'd0634a24-91d0-4fd7-8868-2caa3ab0dc7a' })
WITH n
OPTIONAL MATCH (n)-[r:IS_PART]->(p:KnowledgeEntry)
WITH n, p, r
OPTIONAL MATCH (n)<-[ch:IS_PART*1..2]-(d:HierarchicalKnowledgeEntry)
WITH n, p, d, r, ch
WITH (collect(DISTINCT n) + collect(DISTINCT p) + collect(DISTINCT d)) as nodes, collect(DISTINCT r) as parentRelation, collect(DISTINCT ch) as childRelation
UNWIND nodes AS n
OPTIONAL MATCH (n)-[r:CONTEXTUAL_KNOWLEDGE]-(c:ContextualKnowledgeEntry)
WITH n as node, parentRelation, childRelation, c AS contextualNode, r as contextualRelation
WITH collect(DISTINCT node) + collect(DISTINCT contextualNode) as knowledgeEntries, parentRelation + childRelation + collect(distinct contextualRelation) as edges
return knowledgeEntries, edges