본문 바로가기

Data Analytics

Patterns for relationships in Neo4j

 

 

https://neo4j.com/docs/cypher-manual/4.0/syntax/patterns/#cypher-pattern-relationship

 

Patterns - Neo4j Cypher Manual

The simplest way to describe a relationship is by using the arrow between two nodes, as in the previous examples. Using this technique, you can describe that the relationship should exist and the directionality of it. If you don’t care about the directio

neo4j.com

 

 

6. Patterns for relationships

The simplest way to describe a relationship is by using the arrow between two nodes, as in the previous examples. Using this technique, you can describe that the relationship should exist and the directionality of it. If you don’t care about the direction of the relationship, the arrow head can be omitted, as exemplified by:

Cypher

Copy to Clipboard

(a)--(b)

As with nodes, relationships may also be given names. In this case, a pair of square brackets is used to break up the arrow and the variable is placed between. For example:

Cypher

Copy to Clipboard

(a)-[r]->(b)

Much like labels on nodes, relationships can have types. To describe a relationship with a specific type, you can specify this as follows:

Cypher

Copy to Clipboard

(a)-[r:REL_TYPE]->(b)

Unlike labels, relationships can only have one type. But if we’d like to describe some data such that the relationship could have any one of a set of types, then they can all be listed in the pattern, separating them with the pipe symbol | like this:

Cypher

Copy to Clipboard

(a)-[r:TYPE1|TYPE2]->(b)

Note that this form of pattern can only be used to describe existing data (ie. when using a pattern with MATCH or as an expression). It will not work with CREATE or MERGE, since it’s not possible to create a relationship with multiple types.

As with nodes, the name of the relationship can always be omitted, as exemplified by:

Cypher

Copy to Clipboard

(a)-[:REL_TYPE]->(b)

7. Variable-length pattern matching

Variable length pattern matching in versions 2.1.x and earlier does not enforce relationship uniqueness for patterns described within a single MATCH clause. This means that a query such as the following: MATCH (a)-[r]->(b), p = (a)-[]->(c) RETURN *, relationships(p) AS rs may include r as part of the rs set. This behavior has changed in versions 2.2.0 and later, in such a way that r will be excluded from the result set, as this better adheres to the rules of relationship uniqueness as documented here Cypher path matching. If you have a query pattern that needs to retrace relationships rather than ignoring them as the relationship uniqueness rules normally dictate, you can accomplish this using multiple match clauses, as follows: MATCH (a)-[r]->(b) MATCH p = (a)-[]->(c) RETURN *, relationships(p). This will work in all versions of Neo4j that support the MATCH clause, namely 2.0.0 and later.

Rather than describing a long path using a sequence of many node and relationship descriptions in a pattern, many relationships (and the intermediate nodes) can be described by specifying a length in the relationship description of a pattern. For example:

Cypher

Copy to Clipboard

(a)-[*2]->(b)

This describes a graph of three nodes and two relationship, all in one path (a path of length 2). This is equivalent to:

Cypher

Copy to Clipboard

(a)-->()-->(b)

A range of lengths can also be specified: such relationship patterns are called 'variable length relationships'. For example:

Cypher

Copy to Clipboard

(a)-[*3..5]->(b)

This is a minimum length of 3, and a maximum of 5. It describes a graph of either 4 nodes and 3 relationships, 5 nodes and 4 relationships or 6 nodes and 5 relationships, all connected together in a single path.

Either bound can be omitted. For example, to describe paths of length 3 or more, use:

Cypher

Copy to Clipboard

(a)-[*3..]->(b)

To describe paths of length 5 or less, use:

Cypher

Copy to Clipboard

(a)-[*..5]->(b)

Both bounds can be omitted, allowing paths of any length to be described:

Cypher

Copy to Clipboard

(a)-[*]->(b)

 

Query

Cypher

Copy to ClipboardRun in Neo4j Browser

MATCH (me)-[:KNOWS*1..2]-(remote_friend) WHERE me.name = 'Filipa' RETURN remote_friend.name

8. Assigning to path variables

As described above, a series of connected nodes and relationships is called a "path". Cypher allows paths to be named using an identifer, as exemplified by:

Cypher

Copy to Clipboard

p = (a)-[*3..5]->(b)

 

 

 

 

=======================================================

 

2. Patterns for nodes

The very simplest 'shape' that can be described in a pattern is a node. A node is described using a pair of parentheses, and is typically given a name. For example:

Cypher

Copy to Clipboard

(a)

This simple pattern describes a single node, and names that node using the variable a.

A more powerful construct is a pattern that describes multiple nodes and relationships between them. Cypher patterns describe relationships by employing an arrow between two nodes. For example:

Cypher

Copy to Clipboard

(a)-->(b)

This pattern describes a very simple data shape: two nodes, and a single relationship from one to the other. In this example, the two nodes are both named as a and b respectively, and the relationship is 'directed': it goes from a to b.

This manner of describing nodes and relationships can be extended to cover an arbitrary number of nodes and the relationships between them, for example:

Cypher

Copy to Clipboard

(a)-->(b)<--(c)

Such a series of connected nodes and relationships is called a "path".

Note that the naming of the nodes in these patterns is only necessary should one need to refer to the same node again, either later in the pattern or elsewhere in the Cypher query. If this is not necessary, then the name may be omitted, as follows:

Cypher

Copy to Clipboard

(a)-->()<--(c)

4. Patterns for labels

In addition to simply describing the shape of a node in the pattern, one can also describe attributes. The most simple attribute that can be described in the pattern is a label that the node must have. For example:

Cypher

Copy to Clipboard

(a:User)-->(b)

One can also describe a node that has multiple labels:

Cypher

Copy to Clipboard

(a:User:Admin)-->(b)

5. Specifying properties

Nodes and relationships are the fundamental structures in a graph. Neo4j uses properties on both of these to allow for far richer models.

Properties can be expressed in patterns using a map-construct: curly brackets surrounding a number of key-expression pairs, separated by commas. E.g. a node with two properties on it would look like:

Cypher

Copy to Clipboard

(a {name: 'Andy', sport: 'Brazilian Ju-Jitsu'})

A relationship with expectations on it is given by:

Cypher

Copy to Clipboard

(a)-[{blocked: false}]->(b)

 

 

Match ((p:Person)-[f:FOLLOWS]-(t:Person)) return distinct  f

{ "identity": 243, "start": 167, "end": 168, "type": "FOLLOWS", "properties": { } } 

{ "identity": 242, "start": 168, "end": 169, "type": "FOLLOWS", "properties": { } } 

{ "identity": 241, "start": 170, "end": 169, "type": "FOLLOWS", "properties": { } }

 

 

Match ((p:Person)-[f:FOLLOWS]-(t:Person)) return distinct  f

{ "identity": 243, "start": 167, "end": 168, "type": "FOLLOWS", "properties": { } } 

{ "identity": 242, "start": 168, "end": 169, "type": "FOLLOWS", "properties": { } } 

{ "identity": 243, "start": 167, "end": 168, "type": "FOLLOWS", "properties": { } } 

{ "identity": 241, "start": 170, "end": 169, "type": "FOLLOWS", "properties": { } } 

{ "identity": 242, "start": 168, "end": 169, "type": "FOLLOWS", "properties": { } } 

{ "identity": 241, "start": 170, "end": 169, "type": "FOLLOWS", "properties": { } }

 

 

 

https://ecb30a4b.databases.neo4j.io/browser/?cmd=guide&arg=aura/movies&_ga=2.166482366.1498881835.1636416669-1494959812.1636416669 

 

Neo4j Browser

 

ecb30a4b.databases.neo4j.io

 

 

1 ~ 11 의 강의가 훌륭함