Skip to content
Developer Edition runs on one host and does not require a product license file. Need multi-host deployment or performance testing at scale? Explore Enterprise Edition

Label expression

The label expression references one or more labels of a graph element.

Label expressions are often used in labeled predicates or graph patterns to filter graph elements based on their labels. For more information, see Labeled predicate and Graph patterns.

Syntax

label_expression
label_factor & |
label_factor
label_name % ( label_expression ) ! label_name ! % ! ( label_expression )

label_expression ::=
    (label_factor ( '&' label_factor )* ) ( '|' (label_factor ( '&' label_factor )*) )*

label_factor ::=
    label_name
    | '%'
    | '(' label_expression ')'
    | '!' label_name
    | '!' '%'
    | '!' '(' label_expression ')'           

Parameters

label_name

The name of a label.

%

The wildcard character that matches any label.

!

The negation operator, which indicates the exclusion of a label.

&

The conjunction operator, which indicates the intersection of two label factors.

|

The disjunction operator, which indicates the union of two label factors.

Examples

The following example is based on the example dataset.

Reference one label

'''
USE movie 
MATCH (v)-[e]->() 
WHERE v IS LABELED Person 
RETURN DISTINCT v.name AS name 
LIMIT 5
'''

The preceding statement selects nodes labeled Person and returns the names of the nodes.

Return:

+---------------+
| name          |
+---------------+
| null          |
| Jürgen Tonkel |
| Frank Harper  |
| James Doohan  |
| Fana Mokoena  |
+---------------+

Reference a random label

'''
USE movie  
MATCH (v:%)-[e]->() 
RETURN DISTINCT v.name AS name 
LIMIT 5
'''

The preceding statement selects nodes with any label and returns the names of the nodes.

Return:

+------------------------+
| name                   |
+------------------------+
| Marco Kreuzpaintner    |
| Curt Siodmak           |
| David Lynch            |
| Billy Wilder           |
| Star Trek: Generations |
+------------------------+

Exclude a label

'''
USE movie  
MATCH (v)-[e:!Act]->() 
RETURN DISTINCT e 
LIMIT 2
'''

The preceding statement selects edges that are not labeled Act and returns the information of the edges.

Return:

+----------------------------------------------------------------------+
| e                                                                    |
+----------------------------------------------------------------------+
| (289047300406247427)-[0@Watch:Watch{rate:4.0}]->(289108623949299713) |
| (289047300406247427)-[0@Watch:Watch{rate:1.0}]->(289138379482726401) |
+----------------------------------------------------------------------+

Reference labels with the conjunction operator

'''
USE movie  
MATCH (v WHERE v IS LABELED Person&Movie)-[e]->() 
RETURN DISTINCT v.name AS name 
LIMIT 5
'''

The preceding statement selects nodes labeled Person and Movie and returns the names of the nodes.

Return:

+------+
| name |
+------+
+------+

Reference labels with the disjunction operator

'''
USE movie  
MATCH (v)-[e]->() 
WHERE v IS LABELED Person|Movie 
RETURN DISTINCT v.name AS name 
LIMIT 5
'''

The preceding statement selects nodes labeled Person or Movie and returns the names of the nodes.

Return:

+-------------------------------------+
| name                                |
+-------------------------------------+
| Green Mile                          |
| Willy Wonka & the Chocolate Factory |
| Zatoichi                            |
| The Blue Angel                      |
| Sissi – The Young Empress           |
+-------------------------------------+