Database Reference
In-Depth Information
As you can see, the code doesn't have the
if
statement you used previously to filter re-
lationships by required type. It's much more readable and concise, and it gives the same
result as before:
User has seen movie: Fargo
If you need to be very specific, the Neo4j Core Java API allows filtering by relationship
direction in addition to relationship type. To load only outgoing
HAS_SEEN
relationships
for the starting
user
node, you could use the following snippet:
Iterable<Relationship> allOutgoingHasSeenRelationships =
userJohn.getRelationships(Direction.OUTGOING,
DynamicRelationshipType.withName("HAS_SEEN"));
As you can see, this code is improved by using the more advanced filtering features of the
Neo4j Core Java API. But this is a very simple traversal, only looking at the direct neigh-
bors of the selected node. In the next section, you'll go one step further and traverse the
nodes beyond the direct neighbors by determining what movies John's friends like.
4.1.3. Traversing second-level relationships
Let's take the example to the next level and implement a simple recommendation engine
that finds the movies that John's friends like, but that John hasn't seen.
Looking at the graph, John has two friends: Jack and Kate. Jack has seen
Fargo
and
Alien
,
and Kate has seen
Heat
. But John has already seen
Fargo
, so you'd expect two movies to
result from the traversal:
Alien
and
Heat
.
Figure 4.2
illustrates the nodes and relationships
you'll have to visit during the traversal.