GoofyJames
GoofyJames

Reputation: 47

Neo4j merge properties from a text file

Is it possible to merge properties from a text file with existing nodes in neo4j? My text file is in the form of id, property. The ids match the nodes I already have in neo4j. I am trying to match all the node types which all have an id to the ids of the text file and add the new property. I now tried this but it is taking forever to finish. Moreover can I do it on all nodes not on just Songs nodes - I just leave it as MATCH (c)?

USING PERIODIC COMMIT 500
LOAD CSV FROM 'file:///page_rank.txt' AS  line 
MATCH (c:Songs)
WHERE c.id=toInt(line[0]) 
SET c.pageRank = toFloat(line[1])

Upvotes: 1

Views: 280

Answers (2)

cybersam
cybersam

Reputation: 67019

The probable reason for it "taking forever to finish" is that you do not have an index on :Song(id), and so the MATCH/WHERE clauses have to scan through the Song node to find each id.

You can create the index you need this way:

CREATE INDEX ON :Song(id)

However, if you want to make essentially the same query, but for all nodes (not just Song nodes), you will not be able to use "normal" indexes, as above. Instead, for good performance, you'd need to use explicit indexes, which requires much more effort on your part to use.

Upvotes: 0

Fillard Millmore
Fillard Millmore

Reputation: 326

Yes this is possible.

Let's say I have four existing nodes in my database:

(a:Person {id: 01})

(b:Person {id: 02})

(c:Person {id: 03})

(d:Dog {id: 04})

And let's say I want to merge some properties on them - I don't want to create new nodes - just add these properties to the existing nodes.

So I have a CSV file called 'nodes_prop.csv' that looks like the following:

id,property
01,blue
02,green
03,grey
04,black

If I want to use merge (it sounds to me like you could possibly get away with just using MATCH - SET for your use case) to match on the id's of the nodes and set the property (based on values in CSV file), regardless of the label of the node, I could use the following cypher query:

LOAD CSV WITH HEADERS FROM "file:///nodes_prop.csv" AS line
MERGE (a {id: toInteger(line.id)})
ON MATCH SET a.eye_color=line.property
RETURN *;

Upvotes: 1

Related Questions