Cypher找到类似的节点而不重复匹配

时间:2015-02-27 17:00:58

标签: neo4j cypher

我是cypher的新手。我想找到类似的节点而不重复匹配。

示例数据

CREATE (r1:Repository {id:"repository1"})
CREATE (r2:Repository {id:"repository2"})
CREATE (r3:Repository {id:"repository3"})
CREATE (a1:Actor {id: "actor1"}) 
CREATE (a2:Actor {id: "actor2"}) 
CREATE (a3:Actor {id: "actor3"})
CREATE (o1:Organization {id:"organization1"})
CREATE (o2:Organization {id:"organization2"})
MATCH (a:Repository {id:"repository1"}) MATCH (b:Actor {id: 'actor1'})    
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository1"}) MATCH (b:Actor {id: 'actor2'})    
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository1"}) MATCH (b:Actor {id: 'actor3'})   
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository1"}) MATCH (b:Organization {id:  
 'organization1'}) CREATE (a)-[:IN_ORGANIZATION]->(b)
MATCH (a:Repository {id:"repository2"}) MATCH (b:Actor {id: 'actor1'})    
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository2"}) MATCH (b:Actor {id: 'actor2'}) 
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository2"}) MATCH (b:Organization {id: 
'organization1'}) CREATE (a)-[:IN_ORGANIZATION]->(b)
MATCH (a:Repository {id:"repository3"}) MATCH (b:Actor {id: 'actor3'}) 
CREATE (a)-[:IS_ACTOR]->(b)
MATCH (a:Repository {id:"repository3"}) MATCH (b:Organization {id: 
'organization2'}) CREATE (a)-[:IN_ORGANIZATION]->(b)

Cypher支架

MATCH (a)-[r1:IS_ACTOR|IN_ORGANIZATION]->(match)<-   
[r2:IS_ACTOR|IN_ORGANIZATION]-(b) 
where not a.id = b.id  with a,b,count(match) as count, collect (match.id) as 
connections, collect (type(r1)) as rel1 
return a.id,b.id,count,connections,rel1 order by count desc

结果

 a.id           b.id        count  connections                     rel1
 repository2    repository1 3      actor1,actor2,organization1    IS_ACTOR, IS_ACTOR,IN_ORGANIZATION
 repository1    repository2 3      actor1,actor2,organization1    IS_ACTOR, IS_ACTOR,IN_ORGANIZATION
 repository3    repository1 1      actor3                         IS_ACTOR
 repository1    repository3 1      actor3                         IS_ACTOR

如何删除第2行&amp;结果是#4?

基于对similar question的响应,我尝试使用过滤器,但我收到语法错误(下面的密码)

MATCH (a)-[r1:IS_ACTOR|IN_ORGANIZATION]->(match)<-  
[r2:IS_ACTOR|IN_ORGANIZATION]-(b) 
with filter(x in connections where x <> b.id) 
where not a.id = b.id  with a,b,count(match) as count, collect (match.id) as   
connections, collect (type(r1)) as rel1 
return a.id,b.id,count,connections,rel1 order by count desc

1 个答案:

答案 0 :(得分:2)

您可以从两侧匹配路径一次,您可以执行某些操作以强制仅返回其中一个路径。比较id,所以你把a和b放在一个固定的顺序中,避免使用另一个组合。

MATCH (a)-[r1:IS_ACTOR|IN_ORGANIZATION]->(match)
      <-[r2:IS_ACTOR|IN_ORGANIZATION]-(b) 
where id(a) > id(b)
with a,b,count(match) as count, 
     collect (match.id) as connections, collect (type(r1)) as rel1 
return a.id,b.id,count,connections,rel1 order by count desc