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
  •  | 
  •  

Pergunta

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

Foi útil?

Solução

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)

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top