返回2个不同查询之间的公共节点

时间:2017-07-28 06:47:44

标签: neo4j

我想找出两个不同查询之间的公共节点。我正在努力寻找,但想不出解决方案。我的动机是收集前5个节点外节点,收集前5个根节点,并返回前5个outdegree和根节点之间共同的节点。我不知道如何合并结果,因为在使用" return"在第一个查询中的选项,不会执行进一步的语句,但没有"返回"选项我们无法收集结果。 (如果我错了,请纠正我)。以下是查询,

// for root nodes
match (u:Port1)<-[r]-(root)
where not((root)<--())
return distinct(root.id) as Node, count(r) as Outdegree
ORDER BY count(r) desc limit 5

//for outdegree nodes
match (n:Port1)-[r]->()
return n.id as Node, count(r) as Outdegree
order by Outdegree DESC
union
match (a:Port1)-[r]->(leaf)
where not((leaf)-->())
return leaf.id as Node, 0 as Outdegree limit 5

我应该如何组合两个结果,并获得常见节点列表的输出?请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果你只想要两者之间的交集,你甚至不需要工会。只需在两种情况下收集前五名并找到交叉点。

APOC Procedures可以提供帮助。类似的东西:

// for root nodes
match (:Port1)<-[r]-(root)
where not((root)<--())
// no need for distinct since you're aggregating
with root, count(r) as Outdegree
ORDER BY Outdegree desc limit 5
with collect(root) as rootNodes // back to a single row

//for outdegree nodes
match (n:Port1)
// more efficient way to get out degree without needing to expand
with rootNodes, n, size((n)-->()) as Outdegree
order by Outdegree DESC limit 5

with rootNodes, collect(n) as outdegreeNodes
with apoc.coll.intersection(rootNodes, outdegreeNodes) as commonNodes

// if needed, unwind and get outdegree
unwind commonNodes as commonNode
return commonNode.id as Node, size((commonNode)-->()) as Outdegree