Book Image

Neo4j Graph Data Modelling

Book Image

Neo4j Graph Data Modelling

Overview of this book

Table of Contents (16 chapters)
Neo4j Graph Data Modeling
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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             |
+-----------------------------------+
| "UA1262"    | "United"            |
| "AA920"     | "American Airlines" |
+-----------------------------------+
2 rows