Don't fully understand what MATCH (x)--(y)-->(z) yields

I'm using a database from a particular Udemy course. When I run MATCH (x)--(y)-->(z) RETURN x, y, z LIMIT 2 I get

graph3

and when I run MATCH (x)--(y)-->(z) RETURN x, y, z LIMIT 3 (2 -> 3) I get

graph2

As I see it, there are already three, non-repeat instances of (x)--(y)-->(z) in the first diagram—see my graphical annotation—so I don't understand why Neo4j sees the need to add more nodes for the second query. Obviously this means that I don't understand Cypher semantics; I would appreciate it if you would replace my incorrect understanding with a correct one.

When you show the results as a graph in the browser, it seems neo4j shows the nodes that result and all the connections among those nodes. Try viewing your results using the 'text' or 'table' renderings. You will probably be shown the proper number of paths.

1 Like

Not sure what you're saying, but it looks like you're saying that it would make the x, y, z triples that are matched explicit? It might not dispel my doubts, but it's a good idea; will do

The graph rendering seems to show all the nodes that resulted from your query, and displays them will all their connections, regardless if they are part of your result.

The text and table renderings show the explicit result by row; therefore, you should see two results in your first query and three results in the second. If you want to see the connections between them too, you could write the query to return the paths instead of the individual nodes, such as:

MATCH p = (x)--(y)-->(z) RETURN return p LIMIT 3

1 Like

I have three results in the second query, but those three results collectively involve five nodes which is what's unexpected

@glilienfield I guess there's a rule that any match to a MATCH query must consist of at least one component (node or relationship) that doesn't form part of the other matches?

You did not put any constraints on the nodes, so it gave you the first three paths it determined. I just so happens that these paths consisted collectively of 5 nodes. There must be many paths in this data set to choose from. It wasn't guaranteed that you would have gotten the three paths you highlighted in your first diagram, since they are more than three in your dataset. You could get those three if you constrained your node y to be Keanu Reeves (again, assuming there are not more paths traversing through Keanu Reeves)

Add a SKIP clause, and you will get a different set of three paths (assuming you have 6 or paths)

MATCH p = (x)--(y)-->(z)
RETURN return p
SKIP 3
LIMIT 3

1 Like

there is no constraint imposed between the results. Each just has to meet the conditions of the query. Try removing the LIMIT clause, so you see all paths that meet your match pattern.

This is a good reference about path matching:

That makes sense, thanks. So the second query's diagram could've matched the first query's diagram, but it just so happened that it didn't?

Exactly. There were other paths that got determined first and those in diagram did not all make your limit of 3.

1 Like

I made an implicit assumption that because the first two paths were enough to form a third, Cypher would just use that third