UNION

Generic Description

Combine multiple query branches into one result set.

Simple example:

MATCH (a) RETURN a UNION MATCH (b) RETURN b

Consumer-Level Explanation

Use UNION when the query must combine multiple query branches into one result set and the planner needs to see that operation as part of the Cypher row pipeline. Keep the clause explicit because it controls row grain, variable scope, and what later clauses are allowed to reference.

More Detailed Explanation

UNION is useful when several logically different graph patterns should contribute to one output shape. In practice it is common in search-like queries, heterogeneous feeds, or combined procedural result sets.

What this clause is really for:

Advanced Example

This example keeps UNION inside a complete query pipeline so the clause boundary, visible variables, and returned row shape are clear to planner tooling.

PROFILE MATCH (u:User)-[e:VIEWED]->(d:Document)
TIME e.ts BETWEEN datetime('2025-01-01T00:00:00Z') AND datetime('2025-02-01T00:00:00Z')
WITH u, d, cosine_similarity(vector(properties(d).embedding), vector([0.22, 0.18, 0.44])) AS score
RETURN u.user_id, d.title, score
ORDER BY score DESC
LIMIT 10

Real Use Cases

Real Limitations And Tradeoffs