Creating multiple nodes using UNWIND in NEO4J when a node already exist - neo4j

I am using UNWIND to create multiple nodes in NEO4j. The problem is if one of the node is a duplicate it will be rejected and the entire query fails. I want to be able to create multiple relationship between the same nodes if they already exist..e.g a friend could receive multiple invitations from the same person. So I have an array of objects [{email: xxx#mail.com},{email:yyy#mymail.com},...] to be invited and the email of the sponsor sponsorEmail. There is constraint on email so attempts to create a duplicate will fail and reject the entire query. The following works fine when there is no duplicate.
MATCH (s {email: 'sponsor#gmail.com'})
UNWIND $arrayOfObjects as invitees
CREATE (i:Invitee) MERGE (s)-[r:INVITED {since: timestamp()}]->(i)
SET i=invitees
I have tried substituting MERGE the CREATE thinking that the MERGE would find a MATCH and proceed to CREATE the relationship but it did not work..I still get duplicate error. Short of cleaning the arrayOfObjects before executing the query is there another way to do this? What I want is for the duplicate not to fail but to create a relationship with the existing Invitee node.

You need to MERGE the :Invitee node along with the invitee email. As it is now, you are creating empty :Invitee nodes, and only after they are created setting the email address. You need to MERGE them with the email address. (also you should use a label on your first MATCH, otherwise it does an AllNodesScan...I'll assume it's :Invitee for now, but please replace with whatever label makes sense).
MATCH (s:Invitee {email: 'sponsor#gmail.com'})
UNWIND $arrayOfObjects as invitee
MERGE (i:Invitee {email:invitee.email})
CREATE (s)-[r:INVITED {since: timestamp()}]->(i)

[EDITED]
Your MERGE could match any existing Invitee, and your SET could try to change its email value to a non-unique value. This is probably why you get the constraint violation.
If you want only a single INVITED relationship (with the latest timestamp) for each invitee, this query may do what you want:
MATCH (s {email: 'sponsor#gmail.com'})
UNWIND $arrayOfObjects as invitee
MERGE (i:Invitee {email: invitee.email})
ON CREATE SET i = invitee
MERGE (s)-[r:INVITED]->(i)
ON CREATE SET r.since = timestamp()
This query assumes that every map in arrayOfObjects contains a unique email property value. (You should also create an index or uniqueness constraint on :Invitee(email) to speed up the first MERGE.)
The MERGE (s)-[r:INVITED {since: timestamp()}]->(i) clause (which specifies the current timestamp) is flawed, since it would not detect an existing relationship with an older since value -- so it would almost always create a new relationship. The MERGE (s)-[r:INVITED]->(i) clause would only create a relationship if none exists.
Or, if you want to keep track of the timestamps of every invitation, you could make the since value be an array of timestamps, like this:
MATCH (s {email: 'sponsor#gmail.com'})
UNWIND $arrayOfObjects as invitee
MERGE (i:Invitee {email: invitee.email})
ON CREATE SET i = invitee
MERGE (s)-[r:INVITED]->(i)
ON CREATE SET r.since = [timestamp()]
ON MATCH SET r.since = r.since + timestamp()

Both answer submitted are good answers. I am submitting this answer with a few nuances for those who come along later. The main objective of the question was to be able to create multiple invitations to a friend who has not yet accepted and be able to visualize those invitations. The following is what I settled on:
WITH ['tom#abc.com', 'tony#mymail.com',michael#gmail.com'] AS coll
UNWIND coll AS invitee
WITH DISTINCT invitee
MATCH (s:Sponsor {email: 'mary#gmail.com'})
MERGE (i:Invitee {email: invitee})
CREATE (s)-[r:INVITED {since: timestamp()}]->(i)
RETURN r;
This allowed me to create multiple relationships for each invite sent to the same person but only if sent at different times....which I can easily view.

Related

Delete relationships and then add back to node neo4j

I would like to get a node, delete all outgoing relationships of a certain type and then add back relationships.
The problem I have is that once I grab the node, it still maintains it's previous relationships even after delete so instead of having 1 it keeps doubling whatever it has. 1->2->4->8 etc
Sample graph:
CREATE (a:Basic {name:'a'})
CREATE (b:Basic {name:'b'})
CREATE (c:Basic {name:'c'})
CREATE (a)-[:TO]->(b)
CREATE (a)-[:SO]->(c)
The query to delete the previous relationships and then add in the new relationships. (this is just a brief sample where in reality it wouldn't add back the same relationships, but more then likely point it to a different node).
MATCH (a:Basic {name:'a'})
WITH a
OPTIONAL MATCH (a)-[r:TO|SO]->()
DELETE r
WITH a
MATCH (b:Basic {name:'b'})
CREATE (a)-[:TO]->(b)
WITH a
MATCH (c:Basic {name:'c'})
CREATE (a)-[:SO]->(c)
If I change the CREATE to MERGE then it solves the problem, but it feels odd to have to merge when I know that I just deleted all the relationships. Is there a way to update "a" midway through the query so it reflects the changes? I would like to keep it in one query
The behavior you observed is due the subtle fact that the OPTIONAL MATCH clause generated 2 rows of data, which caused all subsequent operations to be done twice.
To force there to be only a single row of data after the DELETE clause, you can use WITH DISTINCT a (instead of WITH a) right after the DELETE clause, like this:
MATCH (a:Basic {name:'a'})
OPTIONAL MATCH (a)-[r:TO|SO]->()
DELETE r
WITH DISTINCT a
MATCH (b:Basic {name:'b'})
CREATE (a)-[:TO]->(b)
WITH a
MATCH (c:Basic {name:'c'})
CREATE (a)-[:SO]->(c)

how to create relationships in loop in neo4j

I am trying to create friend relationship to all the ids in the list but I am getting an error:
Node already exists with label User and property "id"=[2]
Neo.ClientError.Schema.ConstraintViolation
Basically these ids already exist and I just want to create friend relationship to multiple ids at once using for-each.How can I achieve this or is there any other way to do the same? I really appreciate any help.
MATCH (u:User {id:"3"})
FOREACH (id in ["2","4","5"] |
MERGE (u)-[:FRIEND]->(:User {id:id}))
The problem is the usage of MERGE. Merge needs you to bind both ends of the relationship if you don't want either node recreated in the absence of the pattern existing between them.
u is bound, but because there is no FRIEND relation from u to the other users, the entire pattern is created from u, with the FRIEND relation and a new User node.
You can't MATCH the user in FOREACH so instead, use
MATCH (u:User {id:"3"})
match (fb:User)
where fb.id in ["2","4","5"]
MERGE (u)-[:FRIEND]->(fb)
As the users already exist, there is a more simple way :
MATCH (u:User {id:"3"})
MATCH (friends:User) WHERE friends.id IN ["2","4","5"]
MERGE (u)-[:FRIEND]->(friends)

Cypher, create unique relationship for one node

I want to add a "created by" relationship on nodes in my database. Any node should be able of having this relationship but there can never be more than one.
Right now my query looks something like this:
MATCH (u:User {email: 'my#mail.com'})
MERGE (n:Node {name: 'Node name'})
ON CREATE SET n.name='Node name', n.attribute='value'
CREATE UNIQUE (n)-[:CREATED_BY {date: '2015-02-23'}]->(u)
RETURN n
As I have understood Cypher there is no way to achieve what I want, the current query will only make sure there are no unique relationships based on TWO nodes, not ONE. So, this will create more CREATED_BY relationships when run for another User and I want to limit the outgoing CREATED_BY relationship to just one for all nodes.
Is there a way to achieve this without running multiple queries involving program logic?
Thanks.
Update
I tried to simplyfy the query by removing implementation details, if it helps here's the updated query based on cybersams response.
MERGE (c:Company {name: 'Test Company'})
ON CREATE SET c.uuid='db764628-5695-40ee-92a7-6b750854ebfa', c.created_at='2015-02-23 23:08:15', c.updated_at='2015-02-23 23:08:15'
WITH c
OPTIONAL MATCH (c)
WHERE NOT (c)-[:CREATED_BY]-()
CREATE (c)-[:CREATED_BY {date: '2015-02-23 23:08:15'}]->(u:User {token: '32ba9d2a2367131cecc53c310cfcdd62413bf18e8048c496ea69257822c0ee53'})
RETURN c
Still not working as expected.
Update #2
I ended up splitting this into two queries.
The problem I found was that there was two possible outcomes as I noticed.
The CREATED_BY relationship was created and (n) was returned using OPTIONAL MATCH, this relationship would always be created if it didn't already exist between (n) and (u), so when changing the email attribute it would re-create the relationship.
The Node (n) was not found (because of not using OPTIONAL MATCH and the WHERE NOT (c)-[:CREATED_BY]-() clause), resulting in no relationship created (yay!) but without getting the (n) back the MERGE query looses all it's meaning I think.
My Solution was the following two queries:
MERGE (n:Node {name: 'Name'})
ON CREATE SET
SET n.attribute='value'
WITH n
OPTIONAL MATCH (n)-[r:CREATED_BY]-()
RETURN c, r
Then I had program logic check the value of r, if there was no relationship I would run the second query.
MATCH (n:Node {name: 'Name'})
MATCH (u:User {email: 'my#email.com'})
CREATE UNIQUE (n)-[:CREATED_BY {date: '2015-02-23'}]->(u)
RETURN n
Unfortunately I couldn't find any real solution to combining this in one single query with Cypher. Sam, thanks! I've selected your answer even though it didn't quite solve my problem, but it was very close.
This should work for you:
MERGE (n:Node {name: 'Node name'})
ON CREATE SET n.attribute='value'
WITH n
OPTIONAL MATCH (n)
WHERE NOT (n)-[:CREATED_BY]->()
CREATE UNIQUE (n)-[:CREATED_BY {date: '2015-02-23'}]->(:User {email: 'my#mail.com'})
RETURN n;
I've removed the starting MATCH clause (because I presume you want to create a CREATED_BY relationship even when that User does not yet exist in the DB), and simplified the ON CREATE to remove the redundant setting of the name property.
I have also added an OPTIONAL MATCH that will only match an n node that does not already have an outgoing CREATED_BY relationship, followed by a CREATE UNIQUE clause that fully specifies the User node.

Making a relation in neo4j

I am not sure what I am doing wrong here, so here is how I create nodes
CREATE (urlnode_1:UrlNode {url:'url1', nodenumber:1})
CREATE (urlnode_2:UrlNode {url:'url2', nodenumber:2})
I create relations as follows
CREATE
(urlnode_1)-[:OutLink {anchor_text:['MY']}]->(urlnode_2)
Two nodes are created successfully first, now on running the code to create the relation, I would have liked the relation to exist between the two created nodes but it creates two new nodes say 3 and 4 and shows a relation between them. What am i doing wrong here?
To guide you the best way I can, let's sum up some Neo4j basics concerning node and relationships creation :
A node can have one or more labels, labels are meaned to group the nodes by domain (User, Speaker, Company, etc..see a label as a table name for e.g. ). A node can also have properties.
A relationship can have only ONE type, relationships are organizing the graph. Relationships can also have properties.
To create a node, you can use the CREATE writing clause :
CREATE (n:Person {firstname: 'John'})
The CREATE statement will not check if other nodes with same label and properties already exists, it will just create a new node
Relationships can also be created with the same clause :
MATCH (n:Person {firstname: 'John'}), (p:Person {firstname: 'Pierre'})
CREATE (n)-[:KNOWS]->(p)
A complete pattern can also be created in one go :
CREATE (n:Person {name:'Chris'})-[:KNOWS]->(p:Person {name:'Oliver'})
REMINDER : CREATE will not check for existing nodes.
--- AND NOW MERGE ---
MERGE will lazily check for existing nodes, see him as a MATCH OR CREATE clause :
MERGE (n:Person {firstname:'Fred'})
If the node with label Person and firstname Fred does not exist, the node will be created, otherwise nothing will happen. This is where come the handy ON MATCH and ON CREATE mentionned by #joslinm .
If you run this query multiple times after the node creation, your graph will not change, if you know the http protocol, you can say that MERGE is an indempotent request.
Be aware that, MERGE will ensure that an entire pattern exist in the database, by creating it if it does not already exist, meaning that if you do MERGE with a complete pattern, the entire pattern will be looked up for existence, not a single node :
Say a node with label Person and name property with value 'John' already exist in the db :
MERGE (n:Person {name:'John'})
will not affect the graph
However :
MERGE (n:Person {name:'John'})-[:KNOWS]->(:Person {name:'Nathalia'})
A new John node will be created, because the entire pattern does not exist.
It is recommended to use MERGE incrementally :
MERGE (n:Person {name:'John'})
MERGE (p:Person {name:'Nathalia'})
MERGE (n)-[:KNOWS]->(p)
If you want to know more about the MERGE clause, I can highly recommend you this wonderful article from Luanne on GraphAware : http://graphaware.com/neo4j/2014/07/31/cypher-merge-explained.html
Chris
If you create a relationship, a new one will get created every single time. They are not inherently unique. It sounds like you'd rather be merging the relationship; i.e., if they relationship is there, match it, if not, create it.
The merge syntax for it is as follows:
MERGE (a:Node)-[:LIKES]->(b:Node)
ON
MATCH SET a.msg = 'I matched!'
ON
CREATE SET a.msg = 'I created!'
RETURN a
You can try it out here: http://console.neo4j.org/
You'll notice that first the msg will be "I created!" then after it matches, it will be "I matched!"

Neo4j: implementing soft delete with optional relationships

I'm trying to implement a soft delete in Neo4j. The graph described in Cypher from Alice's viewpoint is as such:
(clyde:User)<-[:FOLLOWS]-(alice:User)-[:LIKES]->(bob:User)
Instead of actually deleting a node and its relationships, I'm
changing its label so it can no longer be looked up directly, i.e. dropping its User label and adding a _User label (notice the underscore)
replacing its relationships so it can't be reached anymore by my normal queries, e.g. deleting its :FOLLOWS relationships and replacing it with :_FOLLOWS relationships.
So this is basically the equivalent of moving a row to an archiving table in a relational database. I figured this is a pretty efficient approach because you're effectively never visiting the parts of the graph that have been soft-deleted. Also, you don't have to modify any of your existing queries.
The result of soft-deleting Alice should be this:
(clyde:User)<-[:_FOLLOWS]-(alice:_User)-[:_LIKES]->(bob:User)
My first attempt at the query was this:
match (user:User {Id: 1})
optional match (user)-[follows:FOLLOWS]->(subject)
remove user:User set user:_User
delete follows
create (user)-[:_FOLLOWS]->(subject);
The problem is that when this user is not following anyone, the query tries to create a relationship between user and null because the second match is optional, so it gives me this error: Other node is null.
My second attempt was this:
match (user:User {Id: 1})
remove user:User set user:_User
optional match (user)-[follows:FOLLOWS]->(subject)
foreach (f in filter(f in collect({r: follows, n: subject}) where f.r is not null) | delete f.r create (user)-[:_FOLLOWS]->(f.n));
So I'm putting the relationship and the subject into a map, collecting these maps in a collection, throwing every "empty" map away and looping over the collection. But this query gives me this error:
SyntaxException: Invalid input '.': expected an identifier character, node labels, a property map, whitespace or ')' (line 1, column 238)
Does anyone know how I can fix this?
Thanks,
Jan
Could you change the label first and then match for relationships? Then you should be able to use 'non-optional' match, and not have to deal with the cases where there are no follows relationships, something like
MATCH (user:User {Id: 1})
REMOVE user:User SET user:_User
WITH user
MATCH (user)-[follows:FOLLOWS]->(subject)
DELETE follows
CREATE (user)-[:_FOLLOWS]->(subject)
Or you could carry the user, follows and subject and filter on where subject is not null. Something like
MATCH (user:User {Id: 1})
OPTIONAL MATCH (user)-[follows:FOLLOWS]->(subject)
REMOVE user:User SET user:_User
WITH user, follows, subject
WHERE subject IS NOT NULL
DELETE follows
CREATE (user)-[:_FOLLOWS]->(subject)
Edit:
If the problem is that you want to do this for more than one kind of relationship, then you could try
MATCH (user:User {Id: 1})
REMOVE user:User SET user:_User
WITH user
MATCH (user)-[f:FOLLOWS]->(other)
DELETE f
CREATE (user)-[:_FOLLOWS]->(other)
WITH user LIMIT 1
MATCH (user)-[l:LIKES]->(other)
DELETE l
CREATE user-[:_LIKES]->(other)
You can keep extending it with other relationship types, just be sure to limit user when you carry, since multiple matches (user)-[r]->(other) means there are multiple results for user, or you'll run the next query part multiple times.
I don't think there is a generic way to do it in cypher since you can't dynamically build the relationship type (i.e. CREATE (a)-[newRel:"_"+type(oldRel)]->(b) doesn't work)
Is something like that what you are looking for or am I misunderstanding your question?

Resources