Set a node property as a list or array during csv import

Hello,

I'm using the community edition and my question is similar to: neo4j cypher store array property during csv import - Stack Overflow

The difference here is that my csv is in the form of

id, name, rule
1, part1, 1
1, part1, 2
1, part1, 3
1, part1, 4
2, part2, 1
2, part2, 2
2, part2, 3
2, part2, 4
3, part3, 1
3, part3, 2
3, part3, 3
3, part3, 4
...etc up to id 7000

Currently there are unique nodes for each of the 7000 id's and I need to add a node property for the rules such that the rules are stored in the node as a homogeneous list of numbers. Any given id could have a different number of rules. Essentially, the node property needs to be stored as rule = [1, 2, 3, 4, ..., n].

@mark.needham has a post relevant to my question. After implementing some of this syntax it seems like now I just need to iterate through all of the rule values for each id in order to store them as a list or array.

Here's the cypher query I have so far:

:auto USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS
FROM 'file:///'+$filecsv AS row
MATCH (a:someNode {id: toInteger(row.id)})
FOREACH (value in row.rule|
			SET a.rule = [toInteger(row.rule)])

But this query stores the result as rule = [4]. How can I improve on my cypher query so that I can iterate through all of the rule numbers for each unique id, store them as a rule, and set this list as a node property? Is there a way to do this efficiently in cypher? If not, then I'll have to modify the raw data using Python and use the apoc.convert.fromJsonList() during the ingestion phase.

I've been implementing list functions in my cypher import queries, experimenting with FOREACH, investigating different apoc collection functions but I'm still not quite there.

Thanks,
Jorge

Goodevening,
I could wrong and maybe you need a more advanced solution.

But the first thing i thought was to get name, [1,2,3]:
LOAD CSV etc.... AS x
WITH DISTINCT x.name AS name, COLLECT(DISTINCT x.rules) AS rules
RETURN name, rules
;

Yours Kindly
Omer

2 Likes