neo4j logo neo4j: optional

1 2 3 4 5 6 7 8 9 10
Notes

This was the query from the previous video.

MATCH (n1:Project {name: "tqdm"})-->(n2:Project)
RETURN *

It's a nice query, but it only explorea direct dependencies of tqdm-node. It omits indirect paths. If spacy relies on tqdm then any package that builds on top of spacy also builds on top of tqdm.

Let's explore another query instead.

MATCH path1 = (p1:Project {name: "tqdm"}) --> (p2:Project)
MATCH path2 = (p2) --> (other: Project)
RETURN path1, path2

This will return all paths of length two that start at the tqdm-node. That's interesting, but it unfortunately excludes all paths of length one.

Let's try an alternatively.

MATCH path1 = (p1:Project {name: "tqdm"}) --> (p2:Project)
OPTIONAL MATCH path2 = (p2) --> (other: Project)
RETURN path1, path2

This is an improvement because now the path of length 2 is optional. That means that we're still keeping all the paths of length one.

But what about paths that are even longer?

MATCH path1 = (p1:Project {name: "tqdm"}) --> (p2:Project)
OPTIONAL MATCH path2 = (p2) -[*1..4]-> (other: Project)
RETURN path1, path2

We're using the *[1..4]-syntax here to indicate that we want an optional match of paths of length 1, 2, 3 or 4. It's a very flexible syntax but you should be mindful that the queries might take a fair bit longer if you allow for longer paths.