我想创建一个包含给定节点的所有第一级和第二级邻居的查询。但是,我只想保留一个邻居,如果它在查询中至少有两条边(这样任何只与其余部分连接的节点都被省略了。
结果我将获得所有边缘的列表。
我已经尝试了下面的代码,它让我感兴趣的节点。但我如何获得优势?我尝试在WITH
命令中添加它们,但这没有用。
MATCH (g:User)-[r:KNOWS*0..2]-(p)
WHERE id(g)=410
WITH p as p, count(r) as rC
WHERE rC>=2
RETURN p, rC
我使用this方式生成测试数据
WITH ["Andres","Wes","Rik","Mark","Peter","Kenny","Michael","Stefan","Max","Chris"] AS names
FOREACH (r IN range(0,800) | CREATE (:User {id:r, name:names[r % size(names)]+" "+r}));
match (u:User),(p:User)
with u,p
limit 10000
where rand() < 0.1
create (u)-[:KNOWS]->(p);
答案 0 :(得分:0)
您可以收集路径的关系:
MATCH path = (g:User)-[r:KNOWS*0..2]-(p)
WHERE id(g)=410
WITH p,
count(r) as rC,
collect( relationships(path) ) as edges
WHERE rC>=2
RETURN p, edges, rC
更新
MATCH path = (g:User)-[r:KNOWS*0..2]-(p) WHERE g.id=410
WITH p,
count(r) as rC,
collect( EXTRACT(x in relationships(path) | id(x)) ) as edges
WHERE rC>=2
RETURN p, edges, rC