Syntax
Pattern
Pattern is a graph expression. Pattern is expressed with combination of more than one vertex or edge, so it is very important how to create a pattern with vertex and edge.
Vertex
Vertex is defined by round brackets. If the vertex expressed with vlabel, property, or variable can be more specified and clearer.
( )
Vertex is expressed with ( )
. The above example pattern without any vlabel or property in the round brackets means every vertex.
(:person)
If you want to express vlabel in the vertex, (:vlabelName)
is the right way. The name of vlabel following colon should be written in the round brackets. The above example means a vertex having a vlabel named person.
(v)
(var)
(var_1)
(v:person)
(variableName)
is a way to assign a variable to a vertex. Variable can be named with a combination of alphanumeric(a~z, 0~9) and under-bar. However the variable name cant not be started with number. If you want to express variable and vlabel at the same time, you can write (variableName:vlabelName)
.
({name: 'Jack'})
(v:person {name: 'Jack'})
(v:person {name: 'Jack', age: 24})
If the property is put into the vertex, it can be more specified. For the expression of property, you will write ({perpertyName:propertyValue})
. If the type of value is string, it should be wrapped ' '
. In case of using multiple property, they can be separated by comma(,)
.
Edge
Edge is used with two hyphen, and it can be expressed with elabel, property, and variable together.
-[]-
-[]->
<-[]-
Edge is expressed with -[]-
. If you put the angle brackets at end of side of this expression, you can define the direction of pattern. In the above example -[]-
, there is no constraints, so it means all edges. If the angle brackets added to the above example(e.g., -[]-> or <-[]-), the result with direction will be returned.
-[:knows]->
In order to express other elements on edge, the specified elements should be given in to the square brackets [ ]
(i.e., -[:elabelName]->
for the expression of elabel of edge). The above example means a edge having elabel named knows.
-[e]->
-[e:likes]->
You can use -[variableName]->
expression if you want to add a variable to a edge. The expression of edge with variable and elabel together is like -[variableName:elbelName]->
.
-[{why: 'She is lovely'}]->
-[:likes {why: 'She is lovely'}]->
-[e:likes {why: 'She is lovely'}]->
You can use -[{propertyName:propertyValue}]->
to expresse a property of edge. If the type of value is string, the value should be wrapped with ‘ ‘. It can be multiple with comma
.
Vertices and Edges
Pattern can express vertex and edge either individually or combined.
()-[]->()
(jack:person {name: 'Jack'})-[k:knows]->(emily:person {name: 'Emily'})
p = (:person)-[:knows]->(:person)
The basic pattern of vertex and edge combination is ( )-[]->( )
. It is recommended to define a property and label for vertex and edge. Variable can be defined separately for vertex and edge, but it is also possible for a pattern. To define a variable for a pattern equal symbol(=)
is used. In the above sample p is the variable of the following pattern.
Path
The total number of vertex and edge can be increased continuously. The unit of pattern is called path.
(a)-[]->( )-[]->(c)
(a)-[*2]->(c)
(a)-[]->( )-[]->( )-[]->(d)
(a)-[*3]->(d)
(a)-[]->( )-[]->( )-[]->( )-[]->(e)
(a)-[*4]->(e)
If there are too many paths in the pattern, it is recommended to abbreviate it for the higher readability and ease of composition. If a edge has to take n-1 paths, you can put n-1 following an asterisk in the square brackets. (i.e., refer the above sample.)
Flexible Length
In some case, the number of edge should be dynamic in a query.
(a)-[*2..]->(b)
(a)-[*..7]->(b)
(a)-[*3..5]->(b)
(a)-[*]->(b)
For the dynamic length of paths, double dots (..)
are used as the above sample. Each above sample means a path having more than 2 edges, a path having less than 7 edges, a path having more than 3 and less than 5 edges, and unlimited length of path.