Arka Roy
Arka Roy

Reputation: 190

Data organisation in graph database

This is more of a logical question rather than technical one. I am asking for data organisation guidance for my requirements. Please keep in mind that I am willing to use a graph database for this purpose (though I am pretty new at that). So guidance in graph database context would be much appreciated.

Let me provide an overview of the scenario. There are two entities in the app, User and House. User can owns a house or rents a house. If an user rents a house, there should be time period mentioned for which the user has rented the house. An user may rent same house for different periods.

Demo Dataset:

A (User) -owns-> H1, H2, H3 (House) - one-liner for brevity
X -rents-> H2 (start=DATE1, end=DATE2)
Y -rents-> H2 (start=DATE3, end=DATE4)
X -rents-> H2 (start=DATE5, end=DATE6) - user rents same house again

I am assuming that User and House would be nodes and owns and rents would be edges. Rent period would be properties of rents edges. Please point out if there is any better way.

Questions:

  1. Is this possible in graph database in general to have multiple edges of same type between two nodes? Should I keep just one edge for rent of a specific user to specific house and add periods? Or should I maintain multiple edges for multiple periods?
  2. Is it possible to query for something like: "fetch all the houses that were empty for a period of 3 months"? This should fetch the houses that have a gap of 3 months between consecutive end and next start dates in rents. These houses may not be empty now.
  3. I have checked neo4j, cayley, dgraph etc. Which may be better with this scenario?

Any guidance of how I should keep the data with relationships would be much appreciated. Have a nice day.

Upvotes: 0

Views: 405

Answers (2)

Luke Feeney
Luke Feeney

Reputation: 21

I think this may be solved, but I would just add that TerminusDB may be useful to assess as part of your process. The reason that I sat this is that you are:

  1. TerminusDB uses an expressive logical schema language that allows anything that is logically expressible. So you could have multiple edges of same type between two nodes. However, data modeling is something of an art - as your question suggests - so it will depend on your domain. (I always think that 'deal' could be an edge or a node depending on context - you can participate in a deal or you could strike a deal with another party).

  2. As TerminusDB is a native revision control database, time bound queries can be relatively straightforward. You can get a delta, or a series of deltas, between two events.

Upvotes: 1

Naveen
Naveen

Reputation: 392

There could be a better answer than this, still posting my experience with graph on the given requirement if this of any help to you.

I think it is the best fit for the graph DB for your requirement and to answer your questions.

  1. It is more of designing your graph model to suit the purpose and I think you can have multiple rent edges with different periods from node user to node house. Which way you can maintain the history and you can later delete the older/expired period edges if you want.

    [Just to avoid duplicates] Assume here you need to make sure the edge would be created between nodes (user & house) only if the period slot is free. You can add logic to the query while creating the edge between nodes.

  2. With the given demo data set, here is the sample graph I have created based on the scenario you have described.

http://console.neo4j.org/?id=bxu3sp

Click on the above console link and you can run the below cypher query in the query window at the bottom

    MATCH (user:User)-[rent:RENT]->(house:House) 
    WITH house, rent
    ORDER BY rent.startDate
    WITH collect(rent) as rents, house
    UNWIND range(0, size(rents)-1) as index 
    WITH rents, index, house 
    WHERE duration.inDays(date(rents[index].endDate), date(rents[index+1].startDate)).days > 30 
    RETURN house

This would get the list of houses that was with no allocation for a given period range.

  1. I'm not an expert and never used other than neo4j and so far with my experience on neo4j, documentation is really good and it is powerful with additions like Kafka integrations, GraphQL, Halin monitoring, APOC, etc.

I'd say it is a learning curve, just explore and play around with it to get yourself into the graph DB world.

Update: In case of the same user renting the same house for different periods then the graph would look something like below as said you should avoid creating duplicates by not allowing edges for the same/overlapping window period between any user node and any house node. here in this graph, I have created edges for the different and non-overlapping start/end date so which is valid and not a duplicate.

Graph

Upvotes: 0

Related Questions