Neo4j节点属性数组创建关系

时间:2018-11-04 15:27:41

标签: database graph neo4j cypher

我真的很喜欢Neo4j,但我一直陷在这个小问题上。我的数据具有电影节点,演员节点和ACTED_IN关系。例如:

(a:Actor {name: 'Leonardo DiCaprio'})-[r:ACTED_IN]->(m:Movie {name: 'Titanic'})

但是,有些电影将演员以逗号分隔的数组嵌套在称为cast的属性中。例如:

(m:Movie {name: 'Oceans Eleven',  
   cast: 'George Clooney,Brad Pitt,Andy García,Julia Roberts,Matt Damon'})

如何将转换属性转换为唯一的ACTED_IN关系?

2 个答案:

答案 0 :(得分:2)

此查询需要使用 SPLIT() UNWIND

MATCH (m:Movie {title: 'Oceans Eleven'}) 
WITH m, SPLIT(m.cast, ',') AS list_actors
UNWIND list_actors AS actor
MERGE (m)<-[:ACTED_IN]-(:Actor{name: actor})

然后您可以检查结果:

MATCH p=(m:Movie {title: 'Oceans Eleven'})-[]-(n) RETURN p

enter image description here

答案 1 :(得分:1)

这是一种方法,它还避免创建重复的节点和关系:

MATCH (m:Movie {name: 'Oceans Eleven'})
FOREACH(c IN SPLIT(m.cast, ',') |
  MERGE (a:Actor {name: c})
  MERGE (a)-[r:ACTED_IN]->(m)
);