Database Reference
In-Depth Information
Note
Although all relationships must be directed, the actual traversal can go either way—down
or up the relationship.
When the code in listing 4.1 is run, the resulting node is printed on the screen:
User has seen movie: Fargo
Based on the graph in figure 4.1 , the result is correct. You performed a query of the graph
data using the Neo4j Core Java API.
Note
The Node.getRelationships() methoddoesn'tguaranteetheorderofrelationships
traversed. The order of the movies wasn't important for this example, but if you need
ordered results, ordering would have to be performed as a separate step after traversal.
In the previous example, you filtered the relationships you're interested in by checking
the name of every relationship connected to the starting node representing user John John-
son. Filtering relationships by name is a very common pattern in graph traversals. Neo4j
provides an API that does exactly that, out of the box, so you don't have to repeat the if
clause in the code that checks the relationship types. The following listing illustrates the
improved code using Neo4j's built-in capabilities.
Listing 4.2. Filtering movies using the Neo4j Core Java API filtering capabilities
Search WWH ::




Custom Search