Reputation: 35
I created a time-tree in Neo4j following the approach from:
http://www.markhneedham.com/blog/2014/04/19/neo4j-cypher-creating-a-time-tree-down-to-the-day/
Then I created an email:
CREATE (e:Email {Sender:"A", Recipient:"C"})
afterwards I connected the email to the time-tree:
MATCH
(y:Year {year:2017})-[:HAS_MONTH]->(m:Month {month:2})-[:HAS_DAY]->(d:Day {day:10}),
(e:Email {Sender:"A",Recipient:"C"})
CREATE (e)-[r:SENT_ON]->(Day)
RETURN *
I did this for many specific dates and now i want to Match every email which was sent between the 01.01.2017 and the 31.06.2017
How can I query that?
Upvotes: 2
Views: 154
Reputation: 5047
If you have a time tree, the graph's relationships encode some temporal relations, so you can use the NEXT
relationship.
MATCH
(:Year {year: 2017})-[:HAS_MONTH]->(:Month {month: 1})-[:HAS_DAY]->(d1:Day {day: 1}),
(:Year {year: 2017})-[:HAS_MONTH]->(:Month {month: 6})-[:HAS_DAY]->(d2:Day {day: 30}),
(d1)-[:NEXT*]->(d)-[:NEXT*]->(d2),
(e)-[r:SENT_ON]->(d)
RETURN e
For this specific date range (01.01.2017 and 31.06.2017), the following hack works and is probably quicker than the previous solution:
MATCH
(y:Year {year: 2017})-[:HAS_MONTH]->(m:Month)-[:HAS_DAY]->(d:Day),
(e:Email {Sender:"A", Recipient:"C"}),
(e)-[r:SENT_ON]->(d)
WHERE 1 <= m.month <= 6
RETURN e
For handling dates in general, providing a good solution is more tricky than it sounds. Neo4j does not have a built-in Date/DateTime type yet, so it is typical to store them as epoch timestamps and the APOC library's utility functions to convert them.
If you cannot use APOC for some reason, but need to compare date ranges, a possible workaround is to store dates as numbers in yyyymmdd
format. In that case, you can get dates between 01.01.2017 and 31.06.2017 using the following approach:
WHERE 20170101 <= (10000*y.year + 100*m.month + d.day) <= 20170631
Upvotes: 1