Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle dates in neo4j

I'm an historian of medieval history and I'm trying to code networks between kings, dukes, popes etc. over a period of time of about 50 years (from 1220 to 1270) in medieval Germany. As I'm not a specialist for graph-databases I'm looking for a possibility to handle dates and date-ranges.

Are there any possibilities to handle over a date-range to an edge so that the edges, which represents a relationship, disappears after e.g. 3 years?

Are there any possibility to ask for relationships who have their date-tag in a date-range?

like image 516
Andreas Kuczera Avatar asked Mar 30 '15 10:03

Andreas Kuczera


People also ask

Does Neo4j support datetime?

We don't really care about dates for our query so we'll just use the current time to work around this issue. We can get the current time by calling the datetime() function.

What are the weaknesses of Neo4j?

Neo4j has some upper bound limit for the graph size and can support tens of billions of nodes, properties, and relationships in a single graph. No security is provided at the data level and there is no data encryption. Security auditing is not available in Neo4j.

Can Neo4j handle big data?

Tom's IT Pro in looking at 'Small to Big Data Solutions' notes that graph databases such as Neo4j would be better than Hadoop for some big data problems, particularly those described with networks and its relationships between objects.

How do you create a relationship between two nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.


2 Answers

The common way to deal with dates in Neo4j is storing them either as a string representation or as millis since epoch (aka msec passed since Jan 01 1970).

The first approach makes the graph more easily readable the latter allows you to do math e.g. calculate deltas.

In your case I'd store two properties called validFrom and validTo on the relationships. You queries need to make sure you're looking for the correct time interval.

E.g. to find the king(s) in charge of France from Jan 01 1220 to Dec 31st 1221 you do:

MATCH (c:Country{name:'France'})-[r:HAS_KING]->(king)
WHERE r.validFrom >= -23667123600000 and r.validTo <=-23604051600000
RETURN king, r.validFrom, r.validTo

addendum

Since Neo4j 3.0 there's the APOC library which provides couple of functions for converting timestamps to/from human readable date strings.

like image 140
Stefan Armbruster Avatar answered Sep 21 '22 15:09

Stefan Armbruster


You can also store the dates in their number representation in the following format: YYYYMMDD

In your case 12200101 would be Jan 1st 1220 and 12701231 would be Dec 31st 1270.

It's a useful and readable format and you can perform range searches like:

MATCH (h:HistoricEvent)
WHERE h.date >= 12200101 AND h.date < 12701231
RETURN h

It would also let you order by dates, if you need to.

like image 22
Amin Abu-Taleb Avatar answered Sep 23 '22 15:09

Amin Abu-Taleb