Take the 2-minute tour ×

In the below query, does the 2nd match pattern john-[r?:HAS_SEEN]->(movie) run on the result of the first match john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie) . I am trying to understand if this is similar to the unix pipe concept i.e. the result of the 1st pattern is the input to the 2nd pattern.

start john=node(1)
where r is null
return movie;
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I don't think I would compare multiple MATCH clauses to the UNIX pipes concept. Using multiple, comma-separated matches is just a way of breaking out of the 1-dimensional constraint of writing relationships with a single sentence. For example, the following is completely valid:

MATCH a--b, 

At the very end I went back and referenced a and c even though they weren't used in the clause directly before. Again, this is just a way of drawing 2 dimensions' worth of relationships by only using 1-dimensional sentences. We're drawing a 2-dimensional picture with several 1-dimensional pieces.

On a side note, I WOULD compare the WITH clause to UNIX pipes -- I'd call them analogous. WITH will pipe out any results it finds into the next set of clauses you give it.

share|improve this answer
So how would you read your MATCH clause. a--b AND b--c AND c--d AND d--e AND a--c? Confusion is this. The query I mentioned in my post is supposed to return all the movies that john's friends have seen but not seen by me. It would be good if you can explain how to interpret your example –  Pangea May 10 '13 at 1:48
You could read it like that, if you want. There is semantically no difference between a--b--c and a--b, b--c. They mean the same thing: "a is related to b and be is related to c". The only difference syntactically is that the first one combines two facts together into one sentence. Often this is a reasonable approach because often you're traveling on one non-branching path; however, if you need to fork out in two directions you can't do it in a single line, hence using two lines instead. –  ean5533 May 10 '13 at 2:11
And to answer your other question, yes, your query looks correct for "movies that john's friends have seen but john has not". If you aren't getting the results you expect, you could try putting up a sample dataset on console.neo4j.org and let us take a crack at it. Alternatively, you could try removing the second part of your MATCH and remove your WHERE clause, and replace them both with this: WHERE not(john-[:HAS_SEEN]->(movie)) –  ean5533 May 10 '13 at 2:16
In your example, there are two branches that you are trying to match right a-b-c-d-e and a-c. –  Pangea May 10 '13 at 2:23
Yes, and you could write it as a--b--c--d--e, a--c and get the same thing –  ean5533 May 10 '13 at 4:34
add comment

Yes, simply think of these two matches to be one - i.e.

match (movie)<-[r?:HAS_SEEN]-john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie)


match john-[:IS_FRIEND_OF]->(user)-[:HAS_SEEN]->(movie)<-[r?:HAS_SEEN]-john
share|improve this answer
Could you pls try to explain step-by-step, how one of those queries are executed to derive the result. –  Pangea May 10 '13 at 2:29
add comment

Your Answer


