ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Cypher Fundamentals-Creating Relationships

2022-05-05 23:31:47  阅读:259  来源: 互联网

标签:Relationships Cypher Creating relationship Fundamentals create MERGE between nod


Cypher Fundamentals

Reading Data from Neo4j

Writing Data to Neo4j

QUIZ

Creating Relationships

VideoTranscript  

Creating a relationship between two nodes

In this lesson you will learn how to write Cypher clauses to create relationships between existing nodes in the graph.

Just like you can use MERGE to create nodes in the graph, you use MERGE to create relationships between two nodes. First you must have references to the two nodes you will be creating the relationship for. When you create a relationship between two nodes, it must have:

  • Type

  • Direction

For example, if the Person and Movie nodes both already exist, we can find them using a MATCH clause before creating the relationship between them.

cypher  
MATCH (p:Person {name: 'Michael Cain'})
MATCH (m:Movie {title: 'The Dark Knight'})
MERGE (p)-[:ACTED_IN]->(m)

Here we find the two nodes that we want to create the relationship between. Then we use the reference to the found nodes to create the ACTED_IN relationship.

We can confirm that this relationship exists as follows:

cypher  
MATCH (p:Person {name: 'Michael Cain'})-[:ACTED_IN]-(m:Movie {title: 'The Dark Knight'})
RETURN p, m

By default, in Neo4j Browser, the visualization connects nodes that have relationships between them.

Notice also that you need not specify direction in the MATCH pattern since the query engine will look for all nodes that are connected, regardless of the direction of the relationship.

For example, if we specified this relationship pattern:

cypher  
MATCH (p:Person {name: 'Michael Cain'})<-[:ACTED_IN]-(m:Movie {title: 'The Dark Knight'})
RETURN p, m

This query returns no nodes since there are no nodes with the ACTED_IN relationship to Person nodes in the graph.

Creating nodes and relationships using multiple clauses

We can also chain multiple MERGE clauses together within a single Cypher code block.

cypher  
MERGE (p:Person {name: 'Chadwick Boseman'})
MERGE (m:Movie {title: 'Black Panther'})
MERGE (p)-[:ACTED_IN]-(m)

This code creates two nodes and a relationship between them. Because we have specified the variables p and m, we can use them in the code to create the relationship between the two nodes.

Note that in this MERGE clause where we create the relationships, we did not specify the direction of the relationship. By default, if you do not specify the direction when you create the relationship, it will always be assumed left-to-right.

We can confirm that this relationship exists as follows:

cypher  
MATCH (p:Person {name: 'Chadwick Boseman'})-[:ACTED_IN]-(m:Movie {title: 'Black Panther'})
RETURN p, m

Using MERGE to create nodes and a relationship in single clause

What MERGE does is create the node or relationship if it does not exist in the graph.

This code successfully creates the nodes and relationship:

cypher  
MERGE (p:Person {name: 'Emily Blunt'})-[:ACTED_IN]->(m:Movie {title: 'A Quiet Place'})
RETURN p, m

You can execute this Cypher code multiple times and it will not create any new nodes or relationships.

Check your understanding

1. Creating relationships

From what you have learned thus far, what clause do you use to create a relationship?

  • INSERT

  • ADD

  • ADD RELATIONSHIP

  • MERGE

2. Using MERGE for relationships

Suppose you want to create the LIKES relationship between a reference to a node a, and a reference to a node b where the direction of the relationship is from a to b.

Which statements below will create the LIKES relationship from a to b?

  • MERGE (a)-[LIKES]→(b)

  • MERGE (a)-[:LIKES]→(b)

  • MERGE (a)-[LIKES]-(b)

  • MERGE (a)-[:LIKES]-(b)

3. Create relationship between two existing nodes

Suppose our graph has a Person node for Lucille Ball and a Movie node for Mame. How do we create the ACTED_IN relationship between these two nodes?

Use the dropdown below complete the code.

Once you have selected your option, click the Check Results query button to continue.

cypher  
MERGE (p:Person {name: 'Lucille Ball'})
(m:Movie {title: 'Mame'})
RETURN p, m
Creating a Node 60% Creating a Relationship  

标签:Relationships,Cypher,Creating,relationship,Fundamentals,create,MERGE,between,nod
来源: https://www.cnblogs.com/z-cm/p/16226913.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有