1

So I work with a neo4j database and I would like to find certain people in this database. People are stored as nodes and have certain properties and relationships to other nodes. Now I have a list (xlsm/csv file) of people I want to find. This list contains about 8000 names as well as other properties I could use for the query.

I manage to search for nodes by using individual names from the list, for example:

MATCH (n)

WHERE n.name = 'Peter'

RETURN n.name, n.age

I also understand that I can see if a property appears in a list of strings:

MATCH (a)

WHERE a.name IN ['Peter', 'Timothy']

RETURN a.name, a.age

However, I cannot create a query by manually entering the thousands of names in the list. So what is the most efficient way to filter by the people I want to find?

Sol
  • 11
  • 3

1 Answers1

0

One way to do this is by using LOAD CSV - normally you'd use it to load data into the graph, but you don't have to. Given a graph:

MERGE (:Person { name: 'Geoff' })
MERGE (:Person { name: 'Timothy' })
MERGE (:Person { name: 'Peter' })

And a CSV file dropped into your database's import directory:

Name,Phone
Timothy,07700900100
Peter,07700900101

You could load all the names you're interested in searching for into a list, then look them up using a standard IN clause:

LOAD CSV WITH HEADERS FROM 'file:///names.csv' AS line
WITH collect(line.Name) AS names
MATCH (p: Person) WHERE p.name IN names
RETURN p

enter image description here

Note we've excluded Geoff because he wasn't in the CSV.

An alternative is to try matching each row of the incoming CSV at a time, which makes it easier to filter on multiple properties:

LOAD CSV WITH HEADERS FROM 'file:///names.csv' AS line
MATCH (p: Person) WHERE p.name = line.Name AND p.phone = line.Phone
RETURN p
Pablissimo
  • 2,846
  • 2
  • 19
  • 21