0
votes

I'm new to Neo4j and Cypher and have just imported two csvs - one is a list of places and the other is a list of people.

Each place has multiple peopleIDs for the people that have visited it stored as integers in an array.

Similarly, Each person has multiple placeIDs for the places they have visited stored as integers in an array.

I'm struggling when creating the relationships between the people and place nodes in my graph. I have tried the following, which works for individual IDs, but it doesn't create any relationships from an array of IDs:

MATCH (p1:People),(p2:Place)
WHERE p1.placeID = p2.placeID
CREATE (p1)-[:VISIT]->(p2)

The headers for the People csv file are as follows: People csv headers

The headers for the Places csv file are as follows: enter image description here

I guess that I need to use FOREACH or UNWIND or both but I can't find any good examples of how to achieve what I'm after

2

2 Answers

0
votes

You did not use the optimal strategy for importing the data. In a graph DB, it is not good practice to store data (like lists of IDs) that would only be used to create relationships later. That would leave you with a lot of redundant data that would never be used again (and ideally should be deleted).

A better strategy would involve 3 steps:

  1. Import the nodes from the People file - but ignore the Place ID lists

  2. Import the nodes from the Place file - but ignore the People ID lists

  3. Import the relationships from one of the files (I assume it does not matter which). For example, I'll assume we pick the Place file, and that each file row has an id field with the ID of a Place and a people field containing a semicolon-delimited collection of People IDs. A query like this would then create all the needed relationships (and avoid creating duplicates):

    LOAD CSV WITH HEADERS FROM 'file:///places.csv' AS row
    MATCH (place:Place) WHERE place.id = row.id
    MATCH (person:People) WHERE person.id IN SPLIT(row.peopleIds, ';')
    MERGE (person)-[:VISIT]->(place)
    
0
votes

I've managed to track down a similar question to my one here, which provides an answer here

To solve my question I've used the following:

MATCH (p1:People)
UNWIND p1.placeID as pID
MERGE (p2:Place{placeID: pID})
MERGE (p1)-[AT]->(p2)