Neo4j creating duplicate relationships - neo4j

I am very very new to stack overflow and I am trying to upload a csv file for further querying, however, I noticed the file creates duplicates for the relationships, this is the code I have so far, I have read documentation but can't seem to find the solution for duplicate relationships. Please help.
:auto USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///senti.csv" AS csvline
CREATE (:User {username: csvline.username})
CREATE (:Tweet {tweet_id: csvline.tweet_id, text: csvline.text, date_time: csvline.date_time})
CREATE (:Location {place: csvline.location})
CREATE (:Candidate {name: csvline.candidate})
CREATE (:Sentiment {sentiment_polarity: csvline.sentiment});
CREATE INDEX FOR (u:User) ON (u.username);
CREATE INDEX FOR (t:Tweet) ON (t.tweet_id);
CREATE INDEX FOR (l:Location) ON (l.place);
:auto USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///senti.csv" AS csvline
MATCH (u:User {username: csvline.username})
MATCH (t:Tweet {tweet_id: csvline.tweet_id})
MERGE (u)-[:POSTS]->(t);
MERGE (u)-[:BASED_ON]-> (l)

You missed to find location (l) so cypher is creating dummy nodes for l and thus it is creating duplicated relationships :BASED_ON.
:auto USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///senti.csv" AS csvline
MATCH (u:User {username: csvline.username})
MATCH (t:Tweet {tweet_id: csvline.tweet_id})
MATCH (l:Location {place: csvline.location}
MERGE (u)-[:POSTS]->(t)
MERGE (u)-[:BASED_ON]-> (l)

Related

Cannot merge the following node because of null property value for 'name':

My code:
LOAD CSV WITH HEADERS FROM "file:///C:/test1.csv" AS line
MERGE (n:SiteA {name: line.SiteA})
MERGE (m:SiteB {name: line.SiteB})
MERGE (n) -[:has_device_function]-> (m);
my error:
Cannot merge the following node because of null property value for 'name': (:SiteA {name: null})
Could you please help me ?
ry this
LOAD CSV WITH HEADERS FROM "file:///C:/test1.csv" AS line MATCH
(n:SiteA {name: line.SiteA}) where n.name is not null MATCH (m:SiteB
{name: line.SiteB}) where m.name is not null MERGE (n)
-[:has_device_function]-> (m);
Two things to notice: filtering out nulls and using MATCH rather than merge in the initial extraction of the nodes for the new relationship.
In order to easily avoid this kind of problem I usually pass a default value on null items for my CSV (usually DELETE_ME_PLEASE). Then I'll just match and delete nodes with this.
First, just to be sure: this means that there are rows in your CSV that have an empty name column.
That said, you can remove lines from the CSV before continuing your query:
LOAD CSV WITH HEADERS FROM "file:///C:/test1.csv" AS line
WITH line WHERE line.name IS NOT NULL
MERGE (n:SiteA {name: line.SiteA})
MERGE (m:SiteB {name: line.SiteB})
MERGE (n) -[:has_device_function]-> (m);
Csv need coma only and that's work ...

How to create different relationships in neo4j using py2neo reading csv files?

I would like to read in a csv file where the first two columns have node names, and the third column has the node relationship. Currently I use this in py2neo:
query2 = """
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line
MERGE (topic:Topic {name: line.Topic})
MERGE (result:Result {name: line.Result})
CREATE UNIQUE (topic)-[:DISCUSSES]->(result)
"""
How can I use the third column in the csv file to set the relationship, instead of having all relationships set as "DISCUSSES"?
I tried this, but it does not have a UNIQUE option:
query1 = """
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line
MERGE (topic:Topic {name: line.Topic})
MERGE (result:Result {name: line.Result})
MERGE (relation:Relation {name: line.Relation})
WITH topic,result,line
CALL apoc.merge.relationship(topic, line.Relation, {}, {}, result) YIELD rel as rel1
RETURN topic,result
"""
Actually, your second query is almost correct (except that it has an extraneous MERGE clause). Here is the corrected query:
USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line
MERGE (topic:Topic {name: line.Topic})
MERGE (result:Result {name: line.Result})
WITH topic, result, line
CALL apoc.merge.relationship(topic, line.Relation, {}, {}, result) YIELD rel
RETURN topic, result
The apoc.merge.relationship call is equivalent to doing a MERGE to create the relationship (with a dynamic label) if it does not already exist.

Create multiple relationships of same type with different properties between two nodes from csv

I am facing issue while creating multiple relationships of same type with different properties between two nodes in Neo4jDesktop.
Nodes dataset:
File Name: 1.csv
File Contents:
Id,Desc
A,Alpha
B,Beta
C,Charlie
D,Doyce
Relationships Dataset:
File Name: 2.csv
File Contents:
SeqNo,Date,Count,Weight,From,To
0,2018-04-01,12,308,A,B
1,2018-04-01,3,475,B,C
2,2018-04-01,23,308,C,D
3,2018-04-01,32,524,D,A
4,2018-04-01,0,308,A,C
5,2018-04-01,23,237,B,D
6,2018-04-01,54,308,B,A
7,2018-04-01,23,237,D,B
8,2018-04-01,18,308,D,C
9,2018-04-01,23,308,C,A
10,2018-04-01,78,475,B,C
11,2018-04-01,67,308,A,B
12,2018-04-01,56,237,D,B
13,2018-04-01,34,308,A,C
14,2018-04-01,27,524,A,D
15,2018-04-01,84,237,D,B
// Create Nodes
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/1.csv" AS row
CREATE (:Node {Id: row.Id, Desc: row.Desc});
// Create Relationships
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/2.csv" AS row
MERGE (from:Node {Id: row.From})
MERGE (to:Node {Id: row.To})
MERGE (from)-[rel:RELATED_AS]->(to)
ON CREATE SET rel.SeqNo = toInt(row.SeqNo),
rel.Date = row.flightDate,
rel.Count = toInteger(row.Count),
rel.Weight = toFloat(row.Weight)
This syntax works and creates only 11 relationships, with incoming and outgoing relationships between two nodes.
It is ignoring the additional relationships between A-B, B-C, A-C and D-B (2 additional relationships).
How to create the graph with all the 16 relationships?
Thanks in advance.
Mel.
Your second query is MERGING the relationship (from)-[rel:RELATED_AS]->(to) so Cypher is matching that pattern if it exists. So the subsequent ones are matched but then the values are never updated because of the ON CREATE statement.
Since you want to create the relationships every time you could replace your statement with something like the following.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/2.csv" AS row
MERGE (from:Node {Id: row.From})
MERGE (to:Node {Id: row.To})
CREATE (from)-[rel:RELATED_AS {SeqNo: row.SeqNo, Date: row.flightDate, Count: toInteger(row.Count), Weight: toFloat(row.Weight)}]->(to)

Some way to create 1 million relationships with a neo4j query

With this query I am importing 75000 nodes from my csv file. (Category)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///prodcategory.csv" AS row
CREATE (:Category {id: row.idProdCategory, name: row.name, idRestaurant: row.idRestaurant});
And with this query I am also importing 1 million nodes from my csv file (Product)
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (:Product {id: row.idProduct, idProductCategory: row.idProductCategory,name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice});
I am using this query to create the relationship between id -> category and idProductCategory -> products.
MATCH (category:Category {id: category.id})
MATCH (Product:Product {idProductCategory: Product.idProductCategory})
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product);
This query only creates 2999 relationships and I do not believe the 1 million relationships I should create, please if there is a method or configuration to be able to create more than 1 million relationships please help me I would be very grateful.
Ensure you have indexes on Product.idProductCategory.
I assume that the category id is unique across categories.
CREATE CONSTRAINT ON (category:Category) ASSERT category.id IS UNIQUE;
I assume that there are multiple products with the same category ID.
CREATE INDEX ON :Product(idProductCategory);
Then you can simply match each category and then for each category find the appropriate products and create the relationships.
// match all of your categories
MATCH (category:Category)
// then with each category find all the products
WITH category
MATCH (Product:Product {idProductCategory: category.id })
// and then create the
MERGE (category)-[:OF_CATEGORY]->(Product);
If you are running into memory constraints you could use the APOC periodic commit to wrap your query...
call apoc.periodic.commit("
MATCH (category:Category)
WITH category
MATCH (Product:Product {idProductCategory: category.id })
MERGE (category)-[:OF_CATEGORY]->(Product)
",{limit:10000})
try to change your query to this... you are using too many filters in your query
check docs for MATCH
MATCH (category:Category),(Product:Product)
WHERE Product.idProductCategory=category.id
MERGE (category)-[:OF_CATEGORY]->(Product)
you can also just change your second import query, so you do not need a separate query for linking.
USING PERIODIC COMMIT
LOAD CSV WITH HEADERS
FROM "file:///products.csv" AS row
CREATE (p:Product {id: row.idProduct, name: row.name,idRestaurant:row.idRestaurant ,description: row.description, price: row.price, shipping_price: row.shippingPrice})
MATCH (c:Category{id:row.idProductCategory}
MERGE (p)-[:OF_CATEGORY]->(c)

Filter csvlines before inserting the values in cypher

I'm using the LOAD CSV inside cypher to populate my database. I would like to skip lines with blank values in specific field before creating the node in neo4j.
I've tried something like:
LOAD CSV WITH HEADERS FROM "https://dl.dropboxusercontent.com/..../file.csv" AS csvline
MATCH csvline WHERE csvline.name <>""
WITH csvline
MERGE (n {name: csvline.name})
but I get the following error
"Cannot match on a pattern containing only already bound identifiers"
Thank you
Are you using Neo4j 2.1.2 by any chance? I bumped into this error message yesterday.
Try:
LOAD CSV WITH HEADERS FROM "https://dl.dropboxusercontent.com/..../file.csv" AS csvline
WITH csvline
WHERE csvline.name <>""
MERGE ({name: csvline.name});
(Notice you don't need the n identifier since you never use it.)
Cheers.

Resources