Neo4j在给定子节点集的情况下计算连接最紧密的父节点

时间:2018-12-26 12:17:39

标签: neo4j cypher

说我有多棵树:

A<-{D, E}<-F
B<-{E, G}
C<-{E, H}
//Where only A, B, and C are of (:parent{name:""})
//There rest is child

给出一组子节点:

{E, F} //(:child{name:""})
//Clearly A is the most connected parent even if F is not directly connected to A

问题:给定子节点集合,如何查找连接最紧密的父节点?欢迎使用任何密码查询,插件功能或过程。帮助。

这是我尝试过的,但是没有运气,因为它计算了两个节点之间的总体关系:

MATCH (c:child)--(p:parent)
WHERE c.name IN ['E', 'F']
RETURN p ORDER BY size( (p)--(c) ) DESC LIMIT 1
//Also tried size( (p)--() ) but it count all relationship that the parent node has.

2 个答案:

答案 0 :(得分:2)

您缺少的概念是variable-length relationship patterns。有了这个,您就可以从所需的:child节点到:parent节点以可变的距离进行匹配,然后计算父节点出现的次数并占据顶部:

MATCH (c:child)-[*]->(p:parent) // assumes only incoming rels toward :parent
WHERE c.name IN ['E', 'F'] // make sure you have an index on :child(name)
WITH p, count(p) as connected
RETURN p 
ORDER BY connected DESC 
LIMIT 1

答案 1 :(得分:0)

好的,所以我尝试了其他方法,但是不确定在大型图上工作是否有效(比如说2M个节点以上):

MATCH path= shortestPath( (c:child)--(p:parent) )
WHERE c.name IN [...]
WITH p, collect(path) as cnt
RETURN p, size(cnt) AS nchild
ORDER BY nchild DESC LIMIT 1

对此有何看法?