Neo4j扔掉了部分比赛

时间:2018-03-23 10:23:13

标签: neo4j

我需要在Neo4j中运行以下查询:

Match (pnode:Node)-[r:NodeRel]->(to:Node) 
where exists(pnode.label) and exists(to.label) 
with pnode as pnode, collect({edgeLabel: r.label, neighbourId: to.Id}) as neighbours 
return ...

每个节点需要一行,包含邻居列表。它有效。但后来我意识到我还需要to节点满足像pnode这样的模式:

(to:Node)-[:NodeRel]->(to2:Node) where exists(to2.label)

为了获得这个,我尝试了两件事,但都没有效果:

1

Match (pnode:Node)-[r:NodeRel]->(to:Node)-[:NodeRel]->(to2:Node)
where exists(pnode.label) and exists(to.label) and exists(to2.label)
with...

但是然后with子句中的collect通过to2节点的数量复制所有邻居条目(到节点)并且neo4j DOESN&T;你在with子句中使用distinct(!!)

2

Match (pnode:Node)-[r:NodeRel]->(to:Node) 
where exists(pnode.label) and exists(to.label) 
and (to:Node)-[:NodeRel]->(to2:Node) and exists(to2.label)
with...

但" to2未定义"抛出错误。

我也试过这个:

Match (pnode:Node)-[r:NodeRel]->(to:Node)-[:NodeRel]->(to2:Node) 
where exists(pnode.label) and exists(to.label) and exists(to2.label) 
with pnode as pnode, r as r, to as to, collect(to2) as n 
with pnode as pnode, collect({edgeLabel: r.label, neighbourId: to.Id}) as neighbours 
return ...

它给出了正确的结果,但速度很慢。

任何提示??

1 个答案:

答案 0 :(得分:0)

您可以将distinct与聚合函数collect一起使用:

PROFILE
Match (pnode:Node)-[r:NodeRel]->(to:Node)-[:NodeRel]->(to2:Node)
where exists(pnode.label) and exists(to.label) and exists(to2.label)
with pnode, 
     collect(distinct {edgeLabel: r.label, neighbourId: to.Id}) as neighbours 
return ...

但在collect

之前,在性能方面会更好地区分重复项
PROFILE
Match (pnode:Node)-[r:NodeRel]->(to:Node)-[:NodeRel]->(to2:Node)
where exists(pnode.label) and exists(to.label) and exists(to2.label)
with distinct pnode, r, to
with pnode,
     collect({edgeLabel: r.label, neighbourId: to.Id}) as neighbours 
return ...

P.S。如果别名与变量名称相同,则无需再次指定:

  

pnode 为pnode ,r 为r ,...