Cypher 在不重复匹配的情况下找到相似的节点

Cypher to find similar nodes without repeating matches

我是密码新手。我想在不重复匹配的情况下找到相似的节点。

示例数据

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)

密码

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 行和第 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

您从两侧匹配一次路径,您可以执行此操作以强制仅返回其中一个路径。比较 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