databaseneo4jdatabase-schemagraph-databasesschema-design

Graph database schema design - Is this suitable for neo4j?


Scenario: a simple address book where a user can create his own contacts and organize them by adding them in groups. A contact may have multiple addresses.

I have created the following diagram: ![schema-design][1]

I want to query all the contacts who are placed in group x and live in country y.

Is this schema design good enough for those purposes (I want to use the neo4j database)?


Solution

  • It looks like the notion of country should be a first class citizen in your graph since your query depends on it. Graph model design typically gets influenced a lot by your query patterns.

    So I suggest to have a node labeled Country for each country and connect the Address node with :LOCATED_IN relationships to the country. (consequently drop the country property from the address nodes).

    With that change your query is as easy as:

    MATCH (:Group{name:'family'})<-[:placed_in_group]-(contact)-[:lives-at]->()-[:LOCATED_IN]->(:Country{name:'US'})
    RETURN contact