Traversing relationships

Traversing relationships in Neo4j is done by specifying the path that we want to be matched. Queries can be open ended, like the one here, in which we haven't highlighted the direction in which we want the relationship to be traversed. Other open-ended queries might refrain from specifying the relationship type to be traversed or the node labels that would identify the subgraph that needs to be traversed.

Input the following query:

neo4j-sh (?)$ MATCH (source:City {name:"Los Angeles"})->[:HAS_FLIGHT]-(f:Flight)-[:FLYING_TO]->(destination:City {name:"New York"}) RETURN f.code as flight_code, f.carrier as carrier;

The output of this query is as follows:

+-----------------------------------+ | flight_code | carrier | +-----------------------------------+ ...

Get Neo4j Graph Data Modeling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.