mzee
mzee

Reputation: 55

Connect to nodes in a timeline between a start and end point

I created a Needham time tree (http://www.markhneedham.com/blog/2014/04/19/neo4j-cypher-creating-a-time-tree-down-to-the-day/) going down to the hour. On each hour node I stored the epoch time

CREATE (h:Hour {hour: hour, day:day, month: month, year:year, time: apoc.date.parse(year +"-"+month+"-"+day+" "+hour+":00", "s", "yyyy-MM-dd hh:mm")})

Now I want to link every hour between the start and end time on my event node. Something along the lines of

create (e:Event {startHour: <some unix time>, endHour: <some later unix time>})
with e
match (h:Hour) 
FOREACH (hour in range(startHour, endHour) |
    merge (e)-[:IN_HOUR]->(h))

Obviously that won't work because startHour and endTime are epoch times, not an actual hour.

I do have the hours in the time tree connected with [:NEXT] relationships. Could I use that somehow?

Upvotes: 0

Views: 140

Answers (1)

cybersam
cybersam

Reputation: 67044

Your can do this:

CREATE (e:Event {startHour: <some unix time>, endHour: <some later unix time>})
WITH e
MATCH (h:Hour)
USING INDEX h:Hour(time)
WHERE e.startHour <= h.time <= e.endHour
MERGE (e)-[:IN_HOUR]->(h);

The USING INDEX clause gives the Cypher planner a hint that it should use the index (which I assume you already have, for :Hour(time)).

[EDITED]

This will work even if startHour and endHour are not really on an hour. However, if you want startHour to be treated as if it were rounded down to the hour, the WHERE clause can use e.startHour/3600*3600 in place of just e.startHour.

Upvotes: 1

Related Questions