Search code examples
neo4jcypherneo4jclient

Neo4J/Cypher Query assistance


I am doing some research into Graph Database Systems as part of POC to possibly move a system across to. I am really new to the concept and come from a RDBMS background.

I have a model (schema?) that looks like:

Person-[HAS_NAME {type:First|Middle|Last}]-Name
Person-[WAS_BORN_ON]-DateOfBirth
Person-[RESIDES_AT {type:Current|Previous}]-Address

I am able to store this data perfectly using Neo4J and Neo4JClient in a C# application.

Where I am falling flat on my backside is that I want to get out of the store, a list of People and all nodes that are connected to the person (eg their Names, DateOfBirth and Addresses) where certain conditions are met,

I have started with this:

MATCH (dob:DateOfBirth)-[WAS_BORN_ON]-(person:Person)-[HAS_NAME]-(name:Name) WHERE dob.Id = '1954-05-09' RETURN person, dob, name

And it produces something like this which is great:

Full Results

But I want to restrict it to people that have a (last) name of "williams" so I go with this

MATCH (dob:DateOfBirth)-[WAS_BORN_ON]-(person:Person)-[HAS_NAME]-(name:Name) WHERE dob.Id = '1954-05-09' and name.Value = 'Williams' RETURN person, dob, name"

Unfortunately it removes all the other names: Results with Missing Nodes

Unfortunately I want this:

Perfect World


Solution

  • Maybe something like this would work?

    MATCH (dob:DateOfBirth{Id: "1954-05-09"})<-[:WAS_BORN_ON]-(person:Person)
    WITH dob, person
    MATCH (person)-[:HAS_NAME]->(surname:Name{Value: "Williams"})
    WITH person, dob
    MATCH (person)-[:HAS_NAME]->(name:Name)
    RETURN person, dob, name
    

    Edit: Updated query to improve performance as suggested by ulkas.