2

I have a network of nodes which represent People which are connected by relationships (Emails).

The Receiver of the Email is m.slug

Based on: ()-[r]-(m)

I wish to split the property (in this case "Sender" / m.slug ie Larry@google.com)and create a new node "Google.com" AS Company (i.e so I now have a set of Company nodes created from the existing information).

I want to then link Google (the company) to my Person Node (Larry@Google.com).

--

How would you approach this, without creating multiple duplicate Company nodes? (i.e for Sergey@Google.com & Larry@Google.com should be connected to the same Google.com Company Node).

Visual Representation of People and relationships

Example syntax of queries and relationship properties

MShariff
  • 41
  • 3

1 Answers1

1

This is how you can ensure there is a unique Company node for each email address domain name, and associate it (via an AT relationship) with each Person with an email address in that domain. The domain name is lower-cased before storing, to ensure uniqueness, since email addresses frequently come with different casing.

MATCH (n:Person)
MERGE (c:Company {name: TOLOWER(SPLIT(n.slug, '@')[1])})
CREATE (n)-[:AT]->(c);

NOTE: The above query should only be executed ONCE, since the CREATE clause would create the relationship every time, even if it already exists. You can replace CREATE with MERGE if you need to run the query multiple times.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thanks Sam - I tried to run the code above and received _"Neo.ClientError.Statement.SemanticError Cannot merge node using null property value for name"_ -- I tried to then change the name of the query to CompanyName `MATCH (n:Person) MERGE (c:Company {Companyname: TOLOWER(SPLIT(n.slug, '@')[1])}) CREATE (n)-[:AT]->(c);` _However I received the same response and now created two relationship properties (Name & Company Name) that can't be used_ -- No new Nodes have been created in the process (i.e no Company Nodes) Any thoughts? – MShariff Jun 05 '17 at 01:46
  • It seems that not all your `slug` values have a `@` character. You should sanitize your data to make sure that all `slug` values are valid email addresses before making the above query. – cybersam Jun 05 '17 at 03:04