Cypher Fundamentals-Creating Relationships
2022/5/5 23:43:34
本文主要是介绍Cypher Fundamentals-Creating Relationships,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Cypher Fundamentals
Reading Data from Neo4j
- Introduction to Cypher
- Retrieving Nodes
- Finding Relationships
- Traversing Relationships
- Finding Emil
- Filtering Queries
- Finding Specific Actors
Writing Data to Neo4j
- Creating Nodes
- Creating a Node
- Creating Relationships
- Creating a Relationship
- Updating Properties
- Adding Properties to a Movie
- Merge Processing
- Adding or Updating a Movie
- Deleting Data
- Deleting Emil
QUIZ
Creating Relationships
VideoTranscriptCreating 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.
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:
cypherMATCH (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:
cypherMATCH (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.
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:
cypherMATCH (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:
cypherMERGE (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.
cypherMERGE (p:Person {name: 'Lucille Ball'}) -[ACTED_IN]-> -[:ACTED_IN]-> <-[ACTED_IN]- -[ACTED_IN]- (m:Movie {title: 'Mame'}) RETURN p, mCreating a Node 60% Creating a Relationship
这篇关于Cypher Fundamentals-Creating Relationships的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享