0
votes

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

2 Answers

1
votes

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.

0
votes

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.