1
votes

Why do I get duplicate results when using directional relations in my query?

Let's follow Neo4j convention of using movies in examples, I have a director which has two roles in two different movies, one he directed and the other he was a producer:

create (M1:MOVIE {name:'movie 1'}),
       (M2:MOVIE {name:'movie 2'}),
       (D1:DIRECTOR {name:'director 1'}),
       (D1)-[:PRODUCED]->(M2), (D1)-[:DIRECTED]->(M1)

let's get all directors who directed and produced a movie

MATCH (m1:MOVIE)<-[DIRECTED]-(d1:DIRECTOR)-[PRODUCED]->(m2:MOVIE) 
RETURN m1, d1, m2

result is duplicated:

╒══════════════════╤═════════════════════╤══════════════════╕
│"m1"              │"d1"                 │"m2"              │
╞══════════════════╪═════════════════════╪══════════════════╡
│{"name":"movie 2"}│{"name":"director 1"}│{"name":"movie 1"}│
├──────────────────┼─────────────────────┼──────────────────┤
│{"name":"movie 1"}│{"name":"director 1"}│{"name":"movie 2"}│
└──────────────────┴─────────────────────┴──────────────────┘

actually it's also wrong, I expected m1 to be only movies directed by the director, so the second row is wrong!

1

1 Answers

0
votes

When specifying a relationship type you should use : before the relationship type name. You forgot it:

try it (note the : before DIRECTED and PRODUCED):

MATCH (m1:MOVIE) <-[:DIRECTED]- (d1:DIRECTOR) -[:PRODUCED]-> (m2:MOVIE)
RETURN m1, d1, m2

The result:

╒══════════════════╤═════════════════════╤══════════════════╕
│"m1"              │"d1"                 │"m2"              │
╞══════════════════╪═════════════════════╪══════════════════╡
│{"name":"movie 1"}│{"name":"director 1"}│{"name":"movie 2"}│
└──────────────────┴─────────────────────┴──────────────────┘