Adding relationship to existing nodes with Cypher - neo4j

I'm trying out Neo4j for the first time. I'm using the 2.0-RC1 community edition.
I've created some nodes:
MERGE (u:User{username:'admin',password:'admin'})
MERGE (r1:Role{name:'ROLE_ADMIN'})
MERGE (r2:Role{name:'ROLE_WEB_USER'})
MERGE (r3:Role{name:'ROLE_REST_USER'})
and now I want to add relationships between the nodes. However, I don't want to clear out the existing database created with the script above, add the statements and run it again. I want to add relationships to the existing nodes. Google helped me find this:
START n=node(*), m=node(*)
where has(n.username) and has(m.name) and n.username = 'admin'
and m.name = 'ROLE_WEB_USER'
create (n)-[:HAS_ROLE]->(m)
Which works fine (even though I don't understand all the syntax). However, I am aware that this finds any node with a username property and any node with a name property, instead of using labels to check that it has the right type of node.
How can I do the same using labels?

In Neo4j 2.0 you can create schema indexes for your labels and the properties you use for lookup:
CREATE INDEX ON :User(username)
CREATE INDEX ON :Role(name)
To create relationships you might use:
MATCH (u:User {username:'admin'}), (r:Role {name:'ROLE_WEB_USER'})
CREATE (u)-[:HAS_ROLE]->(r)
The MATCH will use an index if possible. If there is no index, it will lookup up all nodes carrying the label and see if the property matches.
N.B. the syntax above will only work with Neo4j 2.0.0-RC1 and above.

Update as of 4/2020:
The new Cypher syntax is as follows. as 'CREATE INDEX ON' has been deprecated is..
CREATE INDEX FOR (n:Label) ON (n.property)

Related

In Neo4j how can I create relationships between one node an a collection of other nodes?

I have a graph containing some nodes with :SubSystem labels. I want to add a new node with a :Document label and create relationships with each of the existing :SubSystem nodes.
When I execute this statement:
MATCH (s:SubSystem)
CREATE (d:Document {title:'New Document'})
CREATE (d)-[:DEPICTS]->(s);
I was surprised when Neo4j created a new :Document node for each :SubSystem. I have 12 sub-systems, so I ended up with 12 new documents each related to one sub-system. I would have expected this behavior had I written:
MATCH (s:SubSystem)
CREATE (:Document {title:'New Document'})-[:DEPICTS]->(s);
But I was expecting that separating the CREATE clauses would create 1 document then create relationships between that document and each of the sub-systems.
Can someone explain why this doesn't work as I was expecting.
EDIT:
I found a solution. This statement does what I wanted, but I still think my original attempt should have worked.
CREATE (d:Document {title:'New Document'})
WITH d MATCH (s:SubSystem) CREATE (d)-[:DEPICTS]->(s);
A MATCH clause generates one or more result rows (or aborts the query if no results are found). A subsequent read/write clause would be executed once per row. Rearranging the order of the clauses, as you did, is one way to work around that (when possible).

Neo4j match node with Id and create relationship

I have a requirement to match two existing nodes with a specific Id and then create a relationship between these nodes.
Below is my cypher. But when I execute this , I always get no changes done .
MATCH(i:`Mechanical Component`)
where ID(i)=9912
with(i)
match(d:Features{name:"Mechanical Component"})
with(d)
where ID(d)=9934
MERGE (i)-[:FEATURES]->(d)
As Frank Pavageau said in the comments, you made an error in your query by not passing i with d in tha second WITHclause. Here is the corrected query you need:
MATCH(i:Mechanical Component)
where ID(i)=9912
with(i)
match(d:Features{name:"Mechanical Component"})
with(d,i)
where ID(d)=9934
MERGE (i)-[:FEATURES]->(d)
Keep in mind that using the internal id is really not recommended since it's generated and may change (see Should we use the Neo4J internal id?). You should probably use your own unique ID (with constraints) and match your node using this ID.

Relationship not being applied correctly between nodes in Neo4j

I am trying to run the code on this GraphGist to play with the Buendia Family Tree in Neo4j.
When I create two nodes and try to connect them with a relationship, as such:
CREATE (JoseArcadioBuendia:Male {name:'Jose Arcadio Buendia',Gender:'Male'})
then
CREATE (UrsulaIguaran:Female {name:'Ursula Iguaran',Gender:'Female'})
then
CREATE (JoseArcadioBuendia)-[:HUSBAND]->(UrsulaIguaran)
I get this:
Why is this code incorrect? The syntax looks fine and it appears to run for the author of the Gist. Am I doing something incorrectly?
I presume that you did not include all 3 CREATE clauses in a single query.
Cypher identifiers (like JoseArcadioBuendia and UrsulaIguaran) only exist for the life of a single query. The DB does not persist these identifiers. So, if you have a query that contains just CREATE (JoseArcadioBuendia)-[:HUSBAND]->(UrsulaIguaran)), neo4j would not know that the nodes already exist and would therefore create 2 new nodes for you (as well as the relationship).
If you had put all 3 clauses in a single query, you would have seen the results you expected. Otherwise, your query would first have to use MATCH to associate those identifiers with the proper nodes (similar to #Bond's answer).
The third line is incorrect. You are not specifying any nodes. Try the following:
MATCH (a:Male), (b:Female)
WHERE a.name = 'Jose Arcadio Buendia' AND b.name = 'Ursula Iguaran'
CREATE (a)-[:HUSBAND]->(b);

Create a relationship only if not exist in neo4j

I want to add a relationship between two nodes, but only if the relationship does not exist. For example:
The Relationship between node1 and node2 currently exists with these properties: time:41221323,link:www.google.com
I am trying to add a relationship with different properties for example:
time:5344241,link:www.google.com
In this case i want to keep the original properties on the relationship.
You can use the below CQL query:
MATCH(a: startNodeLabel {attributes to match start node})
MATCH(m:endNodeLabel {attributes to match end node})
MERGE(a)-[:relationshipName]->(m)
The above merge statement creates relation between nodes a and m if there is no existing relationship between a and m.
You want either the MERGE or CREATE UNIQUE clause:
http://neo4j.com/docs/stable/query-merge.html
http://neo4j.com/docs/stable/query-create-unique.html
Also note that MERGE comes with additional ON CREATE SET and ON MATCH SET so you can control when properties get set.
I believe if you give either specific properties as part of the match syntax it will not create only if all of the properties match exactly.
Create Unique clause does serve this purpose. Neo4j Documet says
CREATE UNIQUE is in the middle of MATCH and CREATE — it will match
what it can, and create what is missing. CREATE UNIQUE will always
make the least change possible to the graph — if it can use parts of
the existing graph, it will.
START a=node(...), b=node(...)
CREATE UNIQUE (a)-[r:LIKES]-(b)
return a,b;

Neo4j Behaviour of Merge

So I have a use case where periodically I update my graph with complex subgraphs. In these subgraphs there will be nodes which are already in the graph, and nodes which are new. I had thought that Merge should do this, but in fact merge appears to create a new node even if there was already a unique node, if the property specifications are not identical.
E.g. on the Neo4j Console, Suppose that I do:
MERGE (a:Crew {name:'Neo', occupation:'The One'})
MERGE (a:Crew {name:'Adam', occupation:'Mechanic'})
CREATE UNIQUE (a)-[r:KNOWS]->(b)
RETURN *
That causes the console to create a second version of Neo, rather than to simple attach the occupation to the existing version.
This happens even if you use:
CREATE CONSTRAINT ON (p:Crew) ASSERT p.name IS UNIQUE
Although now it just refuses to create anything since it won't match the two neo's as one property is blank, and it isn't allowed to create a new node either.
It creates a second version because it's not unique. You're specifying an additional property, "occupation", which doesn't currently exist in the original Neo node, so it doesn't find a match and therefore creates a new node.
Use this instead:
MERGE (a:Crew {name:'Neo'}) ON CREATE SET a.occupation='The One'
MERGE (a:Crew {name:'Adam'}) ON CREATE SET b.occupation='Mechanic'
MERGE (a)-[r:KNOWS]->(b)
RETURN *
See also: http://docs.neo4j.org/refcard/2.1/

Resources