Cant use Merge inside foreach for existed nodes - neo4j

I am expecting he following query to create nodes (only if exits) and relations by a given source node (1) and a list(2) this way:
MERGE (p1:C9{userId: '1'}) WITH p1, [{userId:"2"}] AS users
FOREACH (user IN users | MERGE
((p1)-[r1:follow]->(:C9 {userId: user.userId})))
Thats the outcome:
Now if I am executing this query again by switching the node id's this way:
MERGE (p1:C9{userId: '2'}) WITH p1, [{userId:"1"}] AS users
FOREACH (user IN users | MERGE
((p1)-[r1:follow]->(:C9 {userId: user.userId})))
We got this:
neo4j duplicated for me the node with id=1. I want it to merge in case of existed nodes.
I expected to see only two nodes connected to each other by merging existed nodes.
any idea what I should fix?
Thanks,
ray.

I normally avoid FOREACH when I can use an UNWIND, so I would start with something like this:
MERGE (p1:C9 {userId: '1'})
WITH p1, [{userId:"2"}] AS users
UNWIND users AS user
MERGE (p1)-[r1:follow]->(:C9 {userId: user.userId})
Sometimes you also want to separate your node creation from your relationship creation. If you do both at the same time, I think that Neo4j can think that you want a unique combination of node (with properties) and relationship.
MERGE (p1:C9 {userId: '1'})
WITH p1, [{userId:"2"}] AS users
UNWIND users AS user
MERGE (p2:C9 {userId: user.userId})
MERGE (p1)-[r1:follow]->(p2)

You can use MERGE within FOREACH.
But you have to understand the semantics of MERGE. It tries to MATCH a full pattern and if it does not find it it will fully CREATE that pattern.
You in your case you try to find a pattern within the context of p1 and not globally and if not found it will create it within the context of p1.
So if you change your query to:
MERGE (p1:C9{userId: '2'})
WITH p1, [{userId:"1"}] AS users
FOREACH (user IN users |
MERGE (p2:C9 {userId: user.userId})
MERGE (p1)-[r1:follow]->(p2)
)
I.e. create p2 first and then MERGE the relationship, it will work.

Related

How to do this in a single Cypher Query?

So this is a very basic question. I am trying to make a cypher query that creates a node and connects it to multiple nodes.
As an example, let's say I have a database with towns and cars. I want to create a query that:
creates people, and
connects them with the town they live in and any cars they may own.
So here goes:
Here's one way I tried this query (I have WHERE clauses that specify which town and which cars, but to simplify):
MATCH (t: Town)
OPTIONAL MATCH (c: Car)
MERGE a = ((c) <-[:OWNS_CAR]- (p:Person {name: "John"}) -[:LIVES_IN]-> (t))
RETURN a
But this returns multiple people named John - one for each car he owns!
In two queries:
MATCH (t:Town)
MERGE a = ((p:Person {name: "John"}) -[:LIVES_IN]-> (t))
MATCH (p:Person {name: "John"})
OPTIONAL MATCH (c:Car)
MERGE a = ((p) -[:OWNS_CAR]-> (c))
This gives me the result I want, but I was wondering if I could do this in 1 query. I don't like the idea that I have to find John again! Any suggestions?
It took me a bit to wrap my head around why MERGE sometimes creates duplicate nodes when I didn't intend that. This article helped me.
The basic insight is that it would be best to merge the Person node first before you match the towns and cars. That way you won't get a new Person node for each relationship pattern.
If Person nodes are uniquely identified by their name properties, a unique constraint would prevent you from creating duplicates even if you run a mistaken query.
If a person can have multiple cars and residences in multiple towns, you also want to avoid a cartesian product of cars and towns in your result set before you do the merge. Try using the table output in Neo4j Browser to see how many rows are getting returned before you do the MERGE to create relationships.
Here's how I would approach your query.
MERGE (p:Person {name:"John"})
WITH p
OPTIONAL MATCH (c:Car)
WHERE c.licensePlate in ["xyz123", "999aaa"]
WITH p, COLLECT(c) as cars
OPTIONAL MATCH (t:Town)
WHERE t.name in ["Lexington", "Concord"]
WITH p, cars, COLLECT(t) as towns
FOREACH(car in cars | MERGE (p)-[:OWNS]->(car))
FOREACH(town in towns | MERGE (p)-[:LIVES_IN]->(town))
RETURN p, towns, cars

Neo4j create graph that some nodes have more than one relationships

Over my Neo4j I want to create this graph:
SO I tried to create some nodes and relationships with:
MERGE (D:POINT {NAME:'d'})<-[:LINKS]-(A:POINT {NAME:'a'})-[:LINKS]->(B:POINT {NAME:'b'})-[:LINKS]->(C:POINT {NAME:'c'})
But I cannot find out how I will create the relationships between D and B points also I cannot find out how I will link the A and C as well.
Do you have any Idea how to do that?
In order to avoid unintentionally creating duplicate nodes and/or relationships, you must invoke MERGE on individual nodes and relationships.
To quote the dev manual:
When using MERGE on full patterns, the behavior is that either the
whole pattern matches, or the whole pattern is created. MERGE will not
partially use existing patterns — it’s all or nothing. If partial
matches are needed, this can be accomplished by splitting a pattern up
into multiple MERGE clauses.
For example, to properly create your graph without any duplicate nodes or relationships:
MERGE (A:POINT {NAME:'a'})
MERGE (B:POINT {NAME:'b'})
MERGE (C:POINT {NAME:'c'})
MERGE (D:POINT {NAME:'d'})
MERGE (A)-[:LINKS]->(B)
MERGE (A)-[:LINKS]->(C)
MERGE (A)-[:LINKS]->(D)
MERGE (B)-[:LINKS]->(C)
MERGE (D)-[:LINKS]->(B)
CREATE seems to be the natural way to me for creating nodes and relationships.
CREATE (D:POINT {NAME:'d'})<-[:LINKS]-(A:POINT {NAME:'a'})
, (A)-[:LINKS]->(B:POINT {NAME:'b'})<-[:LINKS]-(D)
, (B)-[:LINKS]->(C:POINT {NAME:'c'})<-[:LINKS]-(A)
You can do it by doing a MATCH before a MERGE eg. FOR the relationship between A and D do:
MATCH (A:POINT {NAME:'a'}),(B:POINT {NAME:'d'}) MERGE (A)-[:LINKS]->(B)

Give a specific pattern from a node during clause

I have the nodes: (a:charlie), (b:economy), and (c:bicycle) . I want to create this pattern:
create (a:charlie)-[x:wants_make]->(b:economy)->[y:by_using]->(c:bicycle)
But it gives me cartesian product. I already thought to skip the creation of the node (b) giving to relation [x:want_make]a property. But node (b) has many other relations in the same context(economic context). What I want to get the pattern above.
Any suggestion?
If your query looks like this:
MATCH (a:charlie), (b:economy), (c:bicycle)
MERGE (a)-[:wants_make]->(b)-[:by_using]->(c);
then it is saying both of these things:
Create a wants_make relationship between every charlie node and every economy node.
Create a by_using relationship between every economy node and every bicycle node.
So, if the number of charlie, economy, and bicycle nodes are C, E, and B -- this results in (C * E * B) merges, which is a Cartesian product of a Cartesian product.
Also, your data model seems to be wrong. For example, it seems much more reasonable to have a Person label instead of a charlie label.
A more reasonable query might look something like this:
MERGE (a:Person {name: 'Charlie Brown'})
MERGE (c:Bicycle {id: 123})
MERGE (a)-[:wants_make]->(b:Economy)
MERGE (b)-[:by_using]->(c);
This query avoids Cartesian products by being more specific about the first and last nodes in the path, and it also avoids creating nodes and relationships that already exist.
And, going even further, you might want to combine wants_make, Economy, and by_using into a single economizes_by_using relationship:
MERGE (a:Person {name: 'Charlie Brown'})
MERGE (c:Bicycle {id: 123})
MERGE (a)-[:economizes_by_using]->(c);
You might need to break up your query a bit:
MATCH (a:charlie), (b:economy), (c:bicycle)
MERGE (a)-[:wants_make]->(b), (b)->[:by_using]->(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)

foreach query in cypher to merge similar nodes

I'm trying to figure out how to run through my database, collect nodes that are similar, and then merge them into a single node and re-direct the previous relationships to the newly created node.
I basically created a bunch of nodes in which some or all of the properties had info, like this:
MERGE (company:Company{Name: 'Ford'})
MERGE (person:Person{Name: 'me'})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})
but I accidentally duplicated nodes that should have been merged, and instead created new nodes:
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'', Transmission:''})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:''})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})
MERGE (person)-[:drives]->(car)-[:parent_company]->(company)
so, what I want do do is take the three (car) nodes I accidentally created, merge all of their properties, delete the extra relationships created by extra nodes and correct the path, so (me) would only have a single [:drives] relationship connected to a single (car) connected by a single [:parent_company] relationship.
here is what I tried, but can't quite figure out:
MATCH p=(car:Car{Make:'Ford', Model:'Aerostar'})<-[:drives]-(person:Person{Name:'me'})
FOREACH (car in nodes (p) | SET Car.Color: 'Blue', Car.Transmission:'Auto')
/////This is where I'm stuck
EDIT: Another Attempt(I get "r already declared error):
START n = node(3) //node id for complete 'aerostar' node
WITH n
MATCH (company)<-[:parent_company]-(car:Car{Make:'Ford', Model:'Aerostar')<-[r:drives]-(person)
WITH n, company, r, car, person
MERGE (person)-[r]->(n)-[:parent_company]->(company)
DELETE car
[EDITED]
Does this work for you?
MATCH (car:Car{Make:'Ford', Model:'Aerostar'})<-[d:drives]-(person:Person{Name:'me'})
DELETE d, car
WITH person
CREATE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})<-[d:drives]-(person);
I finally figured out how to do this:
MATCH (car:Car{Make:'Ford', Model:'Aerostar'})<-[d:drives]-(person:Person{Name:'me'})
WITH car,d
SKIP 1
DELETE car,d

Resources