How to create a new relationship in Neo4j, starting from an existing one, using a Cypher query?

StackOverflow https://stackoverflow.com/questions/23597478

  •  20-07-2023
  •  | 
  •  

문제

Is there a simple way to create a new relationship in Neo4j, starting from an existing one?

Starting from the actor-director-movie database used in the tutorials, what I would like to do is to get all the {(actor1),(actor2)} couples of nodes in the graph satisfying the relationships:

(actor1)-[:ACTED_IN]->(movie)<-[:ACTED_IN]-(actor2)

and use them to create a new relationship like:

(actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)

in whatever direction (I am interested in both directed and undirected graphs). Is there a way to do this with a simple Cypher query?

Many thanks,

sTe

도움이 되었습니까?

해결책

Using the sample movie dataset:

MATCH (actor1:Person)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(actor2:Person)
WITH actor1, actor2
MERGE (actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)

다른 팁

I'd do that :

MATCH (actor1)-[:ACTED_IN]->()<-[:ACTED_IN]-(actor2)
CREATE UNIQUE (actor1)-[:ACTED_IN_THE_SAME_MOVIE_AS]-(actor2)

which is basically what you said. Relations are uni-directional (no way around), but the api (Cypher queries or Traversal) can read them both ways (so it doesn't really matter which way your create them in some cases). To check if what you did is ok, you can run the following :

MATCH (actor1)-[:ACTED_IN_SAME_MOVIE]-(actor2)
RETURN actor1, actor2
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top