0

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).

  • The issue is not entirely clear from the question. First, do you have a single person in your graph? Second, where do you count the number of results? The visualization view ("Graph") removes the "duplications", but if you go to the tabular ("Rows"), you should be able to see them. I created a simple example here: https://gist.github.com/szarnyasg/62229f46e81b4a2dd58a5ea26017e561 - please adjust it to your needs and add it to the question. – Gabor Szarnyas Oct 24 '16 at 13:36
  • Many thanks for your comment. I have multiple 'Person's in the database. I know the count because the I inputed that number of nodes for testing purposes –  Oct 24 '16 at 16:01

1 Answers1

4

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:

  1. You have 1 person
  2. You then have 1 person x 13 emails, i.e. 13 (person, email) tuples
  3. You then have 1 person x 13 emails x 10 phones, i.e. 130 (person, email, phone) tuples

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
Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
  • Thanks for your answer. The problem is I can't use the collect function because the retrieved results will not by readable by Neo4jsCliemt when using the .As(Of T) method. Is there anyway we can turn around this? –  Oct 24 '16 at 16:03
  • 1
    The only way to not have as many result rows as you have combinations is to use `collect()`. I have a hard time imagining how a client can't read a collection of strings or numbers as a column, especially a Neo4j client. I mean, an single node property can already contain a collection, even without resorting to `collect()`, so not being able to read those is pretty limiting. – Frank Pavageau Oct 24 '16 at 16:10