0

I was following the Neo4J online tutorial and I came to a question while trying this query with the query tool:

match (a:Person)-[:ACTED_IN|:DIRECTED]->()<-[:ACTED_IN|:DIRECTED]-(b:Person)
return a,b;

I was expecting one of the pairs returned to have the same Person in both identifiers but that didn't happen. Can somebody explain me why? Does a match clause exclude repeated elements in the different identifiers used?

UPDATE: This question came to me in "Lession 3 - Adding Relationships with Cypher, more" from Neo4J online tutorial, where the query I mentioned above is presented. I refined the query to the following one, in order to focus more directly my question:

MATCH (a:Person {name:"Keanu Reeves"})-[:ACTED_IN]->()<-[:ACTED_IN]-(b)
RETURN a,b;

The results:

|---------------|--------------------|
| a             | b                  |
|---------------|--------------------|
| Keanu Reeves  | Carrie-Anne Moss   |
| Keanu Reeves  | Laurence Fishburne |
| Keanu Reeves  | Hugo Weaving       |
| Keanu Reeves  | Brooke Langton     |
| Keanu Reeves  | Gene Hackman       |
| Keanu Reeves  | Orlando Jones      |
|------------------------------------|

So, why there is no row with Keanu Reeves in a and b? Doesn't he should match with both both relations :ACTED_IN?

2 Answers 2

1

The behavior you observed is by design.

To quote the manual:

While pattern matching, Cypher makes sure to not include matches where the same graph relationship is found multiple times in a single pattern. In most use cases, this is a sensible thing to do.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @cybersam, that explains the behavior! I had not read the manual section you mentioned until now.
0

I would check your data sample. Your query looks like it works just fine for me. I replicated with a simple data set, and here's verification that it does produce pairs like what you're looking for.

Joe acted in "Some Flick"

neo4j-sh (?)$ create (p:Person {name:"Joe"})-[:ACTED_IN]->(m:Movie {name:"Some Flick"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Relationships created: 1
Properties set: 2
Labels added: 2
14 ms

But Joe is so multi-talented, he also directed "Some Flick".

neo4j-sh (?)$ match (p:Person {name: "Joe"}), (m:Movie {name: "Some Flick"}) create p-[:DIRECTED]->m;
+-------------------+
| No data returned. |
+-------------------+
Relationships created: 2
23 ms

So who are the actor/director pairs that we know of?

neo4j-sh (?)$ match (a:Person)-[:ACTED_IN|:DIRECTED]->()<-[:ACTED_IN|:DIRECTED]-(b:Person)
> return a,b;
+-----------------------------------------------------+
| a                        | b                        |
+-----------------------------------------------------+
| Node[222128]{name:"Joe"} | Node[222128]{name:"Joe"} |
| Node[222128]{name:"Joe"} | Node[222128]{name:"Joe"} |
+-----------------------------------------------------+
2 rows
50 ms

Of course it's Joe.

2 Comments

Thank you @frobber for the answer. But I suspect that each of the rows matched the nodes a and b with different relations, so if node "a" was matched with relation :ACTED_IN in movie "Some Flick", node "b" was matched with :DIRECTED relation. When you make the query to a Person that for instance only acted in a movie you won't receive a row with that Person in both identifiers. Maybe I didn't express myself correctly...
You might want to update your question then. Also, consider returning the relationships that matched in the return result. That would help us determine whether the relationship that was returned was actually the one you expectd/were looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.