When you need to match nodes or relationships of more than one type in a single query, use the | operator in a Cypher MATCH statement. The | operator acts as a logical OR, returning all graph elements that match any of the specified labels or relationship types — without writing multiple separate queries.
Syntax
Separate multiple labels or relationship types with | in a single pattern:
-- Match a node with either label
(n:Person|Company)
-- Match a relationship with either type
[p:KNOWS|LIKES]Prepare test data
SELECT create_graph('imdb');
SELECT *
FROM cypher('imdb', $$
CREATE (toby:actor {name: 'Toby Maguire'}),
(willam:director {name: 'Willam Dafoe'}),
(hanks:person {name: 'Tom Hanks'}),
(spiderman:movie {title: 'Spiderman'}),
(toby)-[:acted_in]->(spiderman),
(willam)-[:directed]->(spiderman),
(hanks)-[:likes]->(spiderman)
$$) AS (a agtype);Examples
Query nodes with multiple labels
Match nodes that have either the actor or director label:
SELECT * FROM cypher('imdb', $$
MATCH (a:actor|director)
RETURN a.name
$$) AS (name agtype);Result — both actor and director nodes are returned in a single query:
name
----------------
"Toby Maguire"
"Willam Dafoe"
(2 rows)Query relationships with multiple types
Match nodes connected to the movie Spiderman by either an acted_in or directed relationship:
SELECT * FROM cypher('imdb', $$
MATCH (p)-[r:acted_in|directed]->(:movie {title: 'Spiderman'})
RETURN p.name
$$) AS (name agtype);Result — nodes connected by any of the specified relationship types are returned together:
name
----------------
"Toby Maguire"
"Willam Dafoe"
(2 rows)