apoc.path.combine

Function APOC Core

apoc.path.combine(path1, path2) - combines the paths into one if the connecting node matches

Signature

apoc.path.combine(first :: PATH?, second :: PATH?) :: (PATH?)

Input parameters

Name Type Default

first

PATH?

null

second

PATH?

null

Usage Examples

The examples in this section are based on the following sample graph:

MERGE (manUtd:Club {name: 'Man Utd'})
MERGE (juventus:Club {name: 'Juventus'})
MERGE (flamengo:Club {name: 'Flamengo'})

MERGE (premierLeague:League {name: 'Premier League'})
MERGE (serieA:League {name: 'Serie A'})
MERGE (brasileirao:League {name: 'Brasileirão'})

MERGE (england:Country {name: 'England'})
MERGE (brazil:Country {name: 'Brazil'})

MERGE (uefa:Confederation {name: 'UEFA'})

MERGE (manUtd)-[:IN_LEAGUE]->(premierLeague)
MERGE (premierLeague)-[:IN_COUNTRY]->(england)
MERGE (england)-[:IN_CONFEDERATION]->(uefa)

MERGE (juventus)-[:IN_LEAGUE]->(serieA)

MERGE (flamengo)-[:IN_LEAGUE]->(brasileirao)
MERGE (brasileirao)-[:IN_COUNTRY]->(brazil);

If we want to create a path from a query that contains two OPTIONAL MATCH clauses, we can use the apoc.path.combine function.

The following returns a path that combines the (club)-[:IN_LEAGUE]→(league) and (league)-[:IN_COUNTRY]→(country) paths:

MATCH (club:Club)
OPTIONAL MATCH path1 = (club)-[:IN_LEAGUE]->(league)
OPTIONAL MATCH path2 = (league)-[:IN_COUNTRY]->(country)
RETURN club.name, apoc.path.combine(path1, path2) AS path
ORDER BY length(path);
Table 1. Results
club.name path

"Juventus"

(:Club {name: "Juventus"})-[:IN_LEAGUE]→(:League {name: "Serie A"})

"Man Utd"

(:Club {name: "Man Utd"})-[:IN_LEAGUE]→(:League {name: "Premier League"})-[:IN_COUNTRY]→(:Country {name: "England"})

"Flamengo"

(:Club {name: "Flamengo"})-[:IN_LEAGUE]→(:League {name: "Brasileirão"})-[:IN_COUNTRY]→(:Country {name: "Brazil"})

If we want to combine more than two OPTIONAL MATCH paths, see apoc.path.create.