5.1.2 Label Expressions

A label expression in a vertex or an edge element pattern is introduced by the keyword IS.

For example, in the following graph pattern, the vertex pattern associated with the graph element variable v1 has the label person. Also, the edge pattern associated with the graph element variable e contains the label friendOf:

(v1 IS person)–[e IS friendOf]->(v2)

If the label is omitted in a graph element pattern, then the default is to query all vertices or edges.

A label expression can also include an optional in-line SQL search condition that can access any matched variable. When accessing a property, you must specify a graph pattern variable.

The supported vertex and edge label expressions are described in the following table:

Table 5-2 Supported Vertex and Edge Label Expressions

Vertex Label Expression Edge Label Expression Description
(a) [e]
  • The vertex graph pattern variable a may match a vertex with any label.
  • The edge graph pattern variable e may match an edge with any label.
() []
  • The vertex pattern has no label and can match any vertex.
  • The edge pattern has no label and can match any edge.

When a graph pattern variable is not specified, a unique vertex or edge variable name is internally generated by the system. Therefore, you cannot reference the vertex or edge elsewhere in the query, as it is unknown.

(IS person) [IS friend_of]
  • The vertex pattern has only the person label.
  • The edge pattern has only the friend_of label.

When a graph pattern variable is not specified, a unique vertex or edge variable name is internally generated by the system. Therefore, you cannot reference the vertex or edge elsewhere in the query, as it is unknown.

(IS person|place|thing) [IS friend_of|student_of]
  • The vertex pattern has an alternation of three labels, person, place and thing. This implies that the vertex pattern can match any vertex having those labels.
  • The edge pattern has an alternation of two labels, friend_of and student_of. This implies that the edge pattern can match any edge having those labels.

As there is no explicit graph pattern variable in the vertex or edge pattern, you cannot reference this vertex or edge elsewhere in the query.

(a IS person|place|thing) [e IS friend_of|student_of] Same as the preceding table entry. However, the vertex and edge patterns contain a and e as vertex and edge graph pattern variables respectively. Therefore, you can reference the vertex or edge using the respective graph pattern variables elsewhere in the query.

See Example 5-12.

(a IS person),

(a IS car)

(a)–[e IS L1]->(b),

(a)–[e is L2]->(b)

  • The vertex pattern a IS person implies that a must match vertices having the label person, and the vertex pattern a IS car implies that a must match vertices having the label car. Therefore, this represents that a must match vertices having both person and car as labels, effectively an AND of these two conditions. Also, you can reference a vertex as a elsewhere in the query.
  • The edge pattern e IS L1 implies that e must match edges having the label L1, and the edge pattern e IS L2 implies that e must match edges having the label L2. Therefore, this represents that e must match edges having both L1 and L2 as labels, effectively an AND of these two conditions. Also, you can reference an edge as e elsewhere in the query.

See Example 5-13.

(a IS person WHERE a.name = 'Fred') [e IS student_of WHERE e.subject = 'Arts']
  • The vertex pattern has a label person and a vertex graph pattern variable a, which is qualified in the element pattern WHERE clause.
  • The edge pattern has a label student_of and an edge graph pattern variable e, which is qualified in the element pattern WHERE clause.

The only graph pattern variable that is visible within an element pattern is the graph pattern variable defined locally by the element pattern. Graph pattern variables from another element patterns cannot be accessed. See Example 5-5.