How to create a new node with two new outgoing edges? I know how to create one, but I can't figure out how to create a second edge out. Of course, I could do it in a separate MATCH statement, but it seems like it would be cleaner to just create both at once:
LOAD CSV FROM ... as ROW
MATCH (father: Father), (mother: Mother)
WHERE father.id=ROW.father_id, mother.id=ROW.mother_id
CREATE (child: Child{ ... }) ->[:IS_CHILD_OF]->(father)
// what about the IS_CHILD_OF -> mother?
In these cases, you have to be careful with CREATE because when you re-run the query, you may end up with duplications, e.g. of [:IS_CHILD_OF] edges.
Also, when you MERGE a pattern, it will create the entire pattern if a part of it does not exist, e.g. when the father is not in the store.
For these reasons, better to use an approach like below, in which I also suggest to use a :Person label. You may want to add a gender property on those nodes.
LOAD CSV FROM ... as ROW
MERGE (child:Person{id:ROW.child_id})
MERGE (father:Person {id:ROW.father_id})<-[:IS_CHILD_OF]-(child)
MERGE (mother:Person {id:ROW.mother_id})<-[:IS_CHILD_OF]-(child)
simple
CREATE (mother)<-[:IS_CHILD_OF]-(child: Child{ ... })-[:IS_CHILD_OF]->(father)
Related
i m new at neo4j and i d like to upload a csv file and create a set of nodes. However i have already some existing nodes that may exist on that csv file. Is there an option to load the csv, create the nodes based on each row and in case the node already exists skip that row?
Thanks
You can use the MERGE clause to avoid creating duplicate nodes and relationships.
However, you need to carefully read the documentation to understand how to use MERGE, as incorrect usage can cause the unintentional creation of nodes and relationships.
Merge will give you what you want, however you must be careful how you identify the record uniquely to prevent creating duplicates
I'll put the desired final form first as attention spans seem to be on the decline...
// This one is safe assuming name is a true unique identifier of your Friends
// and that their favorite colors and foods may change over time
LOAD CSV FROM 'data/friends.csv' AS line
MERGE (f:Friend { name: line[0]})
set a.favorite_food = line[1]
set a.favorite_color = line[2]
The merge above will create or find the Friend node with that matching name and then, regardless of whether we are creating it or updating it, set the attributes on it.
If we were to instead provide all the attributes in the merge as such:
// This one is dangerous - all attributes must match in order
// to find the existing Friend node
LOAD CSV FROM 'data/friends.csv' AS line
MERGE (f:Friend { name: line[0], favorite_food: line[1], favorite_color: line[2]})
Then we would fail to find an existing friend everytime their favorite_food or favorite_color was updated in our data being (re)loaded.
Here's an example for anyone who's imagination hasn't fully filled in the blanks...
//Last month's file contained:
Bob Marley,Hemp Seeds,Green
//This month's file contained:
Bob Marley,Soylent Green,Rainbow
I just downloaded and installed Neo4J. Now I'm working with a simple csv that is looking like that:
So first I'm using this to merge the nodes for that file:
LOAD CSV WITH HEADERS FROM 'file:///Athletes.csv' AS line
MERGE(Rank:rank{rang: line.Rank})
MERGE(Name:name{nom: line.Name})
MERGE(Sport:sport{sport: line.Sport})
MERGE(Nation:nation{pays: line.Nation})
MERGE(Gender: gender{genre: line.Gender})
MERGE(BirthDate:birthDate{dateDeNaissance: line.BirthDate})
MERGE(BirthPlace: birthplace{lieuDeNaissance: line.BirthPlace})
MERGE(Height: height{taille: line.Height})
MERGE(Pay: pay{salaire: line.Pay})
and this to create some constraint for that file:
CREATE CONSTRAINT ON(name:Name) ASSERT name.nom IS UNIQUE
CREATE CONSTRAINT ON(rank:Rank) ASSERT rank.rang IS UNIQUE
Then I want to display to which country the athletes live to. For that I use:
Create(name)-[:WORK_AT]->(nation)
But I have have that appear:
I would like to know why I have that please.
I thank in advance anyone that takes time to help me.
Several issues come to mind:
If your CREATE clause is part of your first query: since the CREATE clause uses the variable names name and nation, and your MERGE clauses use Name and Nation (which have different casing) -- the CREATE clause would just create new nodes instead of using the Name and Nation nodes.
If your CREATE clause is NOT part of your first query: your CREATE clause would just create new nodes (since variable names, even assuming they had the same casing, are local to a query and are not stored in the DB).
Solution: You can add this clause to the end of the first query:
CREATE (Name)-[:WORK_AT]->(Nation)
Yes, Agree with #cybersam, it's the case sensitive issue of 'name' and 'nation' variables.
My suggesttion:
MERGE (Name)-[:WORK_AT]->(Nation)
I see that you're using MERGE for nodes, so just in case any values of Name or Nation duplicated, you should use MERGE instead of CREATE.
i'm trying to solve a problem of the 1: many relationship display in neo4j. My dataset is as below
child,desc,type,parent
1,PGD,Exchange,0
2,MSE 1,MSE,1
3,MSE 2,MSE,1
4,MSE 3,MSE,1
5,MSE 4,MSE,1
6,BRAS 1,BRAS,2
6,BRAS 1,BRAS,3
7,BRAS 2,BRAS,4
7,BRAS 2,BRAS,5
10,NPE 1,NPE,6
11,NPE 2,NPE,7
12,OLT,OLT,10
12,OLT,OLT,11
13,FDC,FDC,12
14,FDP,FDP,13
15,Cust 1,Customer,14
16,Cust 2,Customer,14
17,Cust 3,Customer,14
LOAD CSV WITH HEADERS FROM 'file:///FTTH_sample.csv' AS line
CREATE(:ftthsample
{child_id:line.child,
desc:line.desc,
type:line.type,
parent_id:line.parent});
//Relations
match (child:ftthsample),(parent:ftthsample)
where child.child_id=parent.parent_id
create (child)-[:test]->(parent)
//Query:
MATCH (child)-[childrel:test*]-(elem)-[parentrel:test*]->(parent)
WHERE elem.desc='FDP'
RETURN child,childrel,elem,parentrel
It returns a display as below.
I want the duplicate nodes to be displayed as one. Newbie with Neo4J. Can anyone of the experts help please?
This seems like an error in your graph creation query. You have a few lines in your query specifying the same node multiple times, but with multiple parents:
6,BRAS 1,BRAS,2
6,BRAS 1,BRAS,3
I'm guessing you actually want this to be a single node, with parent relationships to nodes with the given parent ids, instead of two separate nodes.
Let's adjust your import query. Instead of using a CREATE on each line, we'll use MERGE, and just on the child_id, which seems to be your primary key (maybe consider just using id instead, as a node can have an id on its own, without having to consider the context of whether it's a parent or child). We can use the ON CREATE clause after MERGE to add in the remaining properties only if the MERGE resulted in node creation (instead of matching to an existing node.
That will ensure we only have one node created per child_id.
Rather than having to rematch the child, we can use the child node we just created, match on the parent, and create the relationship.
LOAD CSV WITH HEADERS FROM 'file:///FTTH_sample.csv' AS line
MERGE(child:ftthsample {child_id:line.child})
ON CREATE SET
child.desc = line.desc,
child.type = line.type
WITH child, line.parent as parentId
MATCH (parent:ftthsample)
WHERE parent.child_id = parentId
MERGE (child)-[:test]->(parent)
Note that we haven't added line.parent as a property. It's not needed, since we only use that to create relationships, and after the relationships are there, we won't need those again.
I'm very new to Neo4j:
I'm moving MySQL data to visualise and anaylise the data but I can't set up the relationships.
So far my build script looks like this:
// Create Players
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:player.csv" AS row
CREATE (:Player { playerID: row.id, name: row.Name });
CREATE INDEX ON :Player(name);
CREATE INDEX ON :Player(playerID);
// Create Team
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:team.csv" AS row
CREATE (:Team { teamID: row.id });
CREATE INDEX ON :Team(teamID);
// Create PlayerLinks
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:playerlinks.csv" AS row
CREATE (:Links { linkID: row.id, fromPlayerID: row.fromPlayerID, toPlayerId: row.toPlayerId, teamID: row.teamID, years: row.years });
MATCH (p:Player),(t:Team), (l:Links)
WHERE l.fromPlayerID = p.playerID
AND l.toPlayerId = p.playerID
AND l.teamID = t.teamID
CREATE
The table playerlinks contains the relationships I'd like to create
Here's a diagram of what I'm aiming to achieve:
Looks like you're almost there, actually.
As mentioned in my comments on the question itself, you'll want to drop index creation from your scripts (those should be applied only once before you do your import, and you should consider using unique constraints for ID fields).
As for your :Links nodes, is your plan to only use them to create relationships, or do you plan on keeping them around afterwards?
The approach for keeping :Links around as intermediate nodes with relationships from your :Links nodes to other elements of your graph, might look like this:
MATCH (l:Links)
WITH l
MATCH (p1:Player), (p2:Player), (t:Team)
WHERE l.fromPlayerID = p1.playerID
AND l.toPlayerId = p2.playerID
AND l.teamID = t.teamID
MERGE (l)-[:Teammate]->(p1)
MERGE (l)-[:Teammate]->(p2)
MERGE (l)-[:PlayedOn]->(t)
That connects your :Links node to the players who are teammates, to the :Team they played on, and your :Links node holds the years they played together. At that point you can remove the linkID, toPlayerID, fromPlayerID, and teamID properties from the node, since in a graph db relationships tend to replace foreign keys when translating from a relational db, and since you likely won't be looking up :Links nodes by ID.
Alternately (and according to your desired diagram) you can use the info on the :Links nodes to create relationships between :Players directly. You can set attributes on the relationships for the number of years played together and the ID (or name) of the team they played on. Keep in mind that the relationship itself will not be able to point at the :Team node where the players played together, though you should be able to use that info to create :PlayedOn relationships from the :Players to the :Team in question.
That kind of modeling might look like this:
MATCH (l:Links)
WITH l
MATCH (p1:Player), (p2:Player), (t:Team)
WHERE l.fromPlayerID = p1.playerID
AND l.toPlayerId = p2.playerID
AND l.teamID = t.teamID
MERGE (p1)-[:Teammate{years: l.years, team: t.teamID}]->(p2)
MERGE (p1)-[:PlayedOn]->(t)
MERGE (p2)-[:PlayedOn]->(t)
Keep in mind the MERGING of the :Teammate relationship may be slow. If you only plan on running this only once, you can use CREATE instead of MERGE.
I'm using Neo4j 2.0.0-M06. Just learning Cypher and reading the docs. In my mind this query would work, but I should be so lucky...
I'm importing tweets to a mysql-database, and from there importing them to neo4j. If a tweet is already existing in the Neo4j database, it should be updated.
My query:
MATCH (y:Tweet:Socialmedia) WHERE
HAS (y.tweet_id) AND y.tweet_id = '123'
CREATE UNIQUE (n:Tweet:Socialmedia {
body : 'This is a tweet', tweet_id : '123', tweet_userid : '321', tweet_username : 'example'
} )
Neo4j says: This pattern is not supported for CREATE UNIQUE
The database is currently empty on nodes with the matching labels, so there are no tweets what so ever in the Neo4j database.
What is the correct query?
You want to use MERGE for this query, along with a unique constraint.
CREATE CONSTRAINT on (t:Tweet) ASSERT t.tweet_id IS UNIQUE;
MERGE (t:Tweet {tweet_id:'123'})
ON CREATE
SET t:SocialMedia,
t.body = 'This is a tweet',
t.tweet_userid = '321',
t.tweet_username = 'example';
This will use an index to lookup the tweet by id, and do nothing if the tweet exists, otherwise it will set those properties.
I would like to point that one can use a combination of
CREATE CONSTRAINT and then a normal
CREATE (without UNIQUE)
This is for cases where one expects a unique node and wants to throw an exception if the node unexpectedly exists. (Far cheaper than looking for the node before creating it).
Also note that MERGE seems to take more CPU cycles than a CREATE. (It also takes more CPU cycles even if an exception is thrown)
An alternative scenario covering CREATE CONSTRAINT, CREATE and MERGE (though admittedly not the primary purpose of this post).