Reputation:
I got this Cypher Query:
match (c:Person)
match (c)-[:eml]->(emls:Email)
match (c)-[:phn]->(phns:Phone)
return phns.Number, emls.Value
Number of Email
nodes is 13, Phone
nodes is 10.
When I return the nodes the result is: 13 Email
nodes and 10 Phone
nodes
But when I return a property, let's say: phones.Number
, I get 130 duplicated results (the number came from 13 * 10).
Upvotes: 0
Views: 222
Reputation: 11705
Every time you expand the result graph by matching a new pattern, you perform a cartesian product of the previous results with the new results:
You need to collect at each step to avoid the product: you keep 1 row of result per person, or 1 (person, emails, phones) tuple where both emails and phones are collections.
MATCH (c:Person)
OPTIONAL MATCH (c)-[:eml]->(emls:Email)
WITH c, collect(emls.Value) AS emails
OPTIONAL MATCH (c)-[:phn]->(phns:Phone)
RETURN c, emails, collect(phns.Number) AS phones
Upvotes: 4