Search code examples
neo4jcyphergraph-databases

My match/merge process is not creating relationships in the Neo4J database


I have been trying to follow the Neo4j tutorial to import data I have in a csv and create relationships.

The following code does what I want in terms of reading in the data, creating nodes, and setting properties.

/* Importing data on seller-buyer relationshsips */
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///customer_rel_table.tsv' AS row
FIELDTERMINATOR '\t'
MERGE (seller:Seller {sellerID: row.seller})
    ON CREATE SET seller += {name: row.seller_name,
                             root_eid: row.vendor_eid,
                             city: row.city}
MERGE (buyer:Buyer {buyerID: row.buyer})
    ON CREATE SET buyer += {name: row.buyer_name};


/* Creating indices for the properties I might want to match on */
CREATE INDEX seller_id FOR (s:Seller) on (s.seller_name);
CREATE INDEX buyer_id FOR (b:Buyer) on (b.buyer_name);

/* Creating constraints to guarantee buyer-seller pairs are not duplicated */
CREATE CONSTRAINT sellerID ON (s:Seller) ASSERT s.sellerID IS UNIQUE;
CREATE CONSTRAINT buyerID on (b:Buyer) ASSERT b.buyerID IS UNIQUE;

Now I have the nodes (sellers and buyers) that I want, and I would like to link buyers and sellers. The code I have tried for this is:

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM 'file:///customer_rel_table.tsv' AS row
MATCH (s:Seller {sellerID: row.seller})
MATCH (b:Buyer {buyerID: row.buyer})
MERGE (s)-[st:SOLD_TO]->(b)

The query runs, but I don't get any relationships:

Query executed in 294ms. Query type: WRITE_ONLY. No results.

Since I'm not asking it to RETURN anything, I think the "No results" comment is correct, but when I look at metadata for the DB, no relationships appear. Also, my data has ~220K rows, so 294ms seems fast.

EDIT: At @cybersam's prompting, I tried this query: MATCH p=(:Seller)-[:SOLD_TO]->(:Buyer) RETURN p, which gives No results.

For clarity, there are two fields in my data that are the heart of the relationship: seller and buyer, where the seller sells stuff to the buyer. The seller identifiers are repeated, but for each seller there are unique seller-buyer pairs.

What do I need to fix in my code to get relationships between the sellers and buyers?


Solution

  • Your second query's LOAD CSV clause does not specify FIELDTERMINATOR '\t'. The default terminator is a comma (','). That is probably why it fails to MATCH anything.

    Try adding FIELDTERMINATOR '\t' at the end of that clause.