Skip to content

Instantly share code, notes, and snippets.

@thomasfr
Last active August 29, 2015 13:57
Show Gist options
  • Save thomasfr/9852400 to your computer and use it in GitHub Desktop.
Save thomasfr/9852400 to your computer and use it in GitHub Desktop.
A Neo4J GraphGist about an attempt to model Office Hours. I want to be abel to ask questions like: "What location has opened now or at a given date and time?" and "Has this location open now or at a given date and time?"

Opening / Office/ Business hours in Neo4J

// Create "Location A"
CREATE
  (locationA:Location{name:"Location A"})

// Create opening / business hours for "Location A"
CREATE
  (openingHoursA:OpeningHours)

// Set an validity for the opening hours. 01.01 - 31.12
CREATE
  (openingHoursA)<-[:hasOpeningHours{validFrom:11,validTo:1231}]-(locationA)

CREATE
  (openingHoursA)-[:hasPeriods]->(periodsA:Periods)

// We can set multiple periods.
CREATE
  (periodsA)-[:hasPeriod]->(:Period{from:800,to:1200}),
  (periodsA)-[:hasPeriod]->(:Period{from:1400,to:1900})

// We create a node for each weekday.
// We could also set the validity of the weekday on the "Periods" node itself.
CREATE (monday:Monday:Weekday{name:"Monday",dow:1})
CREATE (tuesday:Tuesday:Weekday{name:"Tuesday",dow:2})
CREATE (wednesday:Wednesday:Weekday{name:"Wednesday",dow:3})
CREATE (thursday:Thursday:Weekday{name:"Thursday",dow:4})
CREATE (friday:Friday:Weekday{name:"Friday",dow:5})
CREATE (saturday:Saturday:Weekday{name:"Saturday",dow:6})
CREATE (sunday:Sunday:Weekday{name:"Sunday",dow:7})

// Set the weekdays where the period set is valid
CREATE
  (periodsA)-[:validOn]->(monday),
  (periodsA)-[:validOn]->(tuesday),
  (periodsA)-[:validOn]->(wednesday),
  (periodsA)-[:validOn]->(thursday),
  (periodsA)-[:validOn]->(friday)


// Create "Location B"
CREATE
  (locationB:Location{name:"Location B"})

// Create opening / business hours for "Location B"
CREATE
  (openingHoursB:OpeningHours)

// Set an validity for the opening hours. 01.01 - 30.11
CREATE
  (openingHoursB)<-[:hasOpeningHours{validFrom:11,validTo:1130}]-(locationB)

CREATE
  (openingHoursB)-[:hasPeriods]->(periodsB:Periods)

// This location just has one period or interval.
CREATE
  (periodsB)-[:hasPeriod]->(:Period{from:800,to:1500})

// Set the weekdays where the period set is valid
CREATE
  (periodsB)-[:validOn]->(monday),
  (periodsB)-[:validOn]->(tuesday),
  (periodsB)-[:validOn]->(wednesday),
  (periodsB)-[:validOn]->(thursday),
  (periodsB)-[:validOn]->(saturday)

// CREATE INDEX ON :Period(from)
// CREATE INDEX ON :Period(to)
// CREATE INDEX ON :Weekday(name)
// CREATE INDEX ON :Weekday(dow)


Graph Output


Get locations which are open at 16:23 #1

MATCH
  (location:Location)-[:hasOpeningHours]->(openingHours:OpeningHours),
  (openingHours)-[:hasPeriods]->(periods:Periods),
  (periods)-[:hasPeriod]->(period:Period)
WHERE
  period.from <= 1623 AND period.to > 1623
RETURN location.name


Get locations which are open at 16:23 #2

MATCH
  (period:Period)
WHERE
  period.from <= 1623 AND period.to > 1623
WITH
  period
MATCH
  (period)<-[:hasPeriod]-(:Periods)<-[:hasPeriods]-(:OpeningHours)<-[:hasOpeningHours]-(location:Location)
RETURN location.name


Get locations which are open on friday

MATCH
  (:Friday)<-[:validOn]-(:Periods)<-[:hasPeriods]-(:OpeningHours)<-[:hasOpeningHours]-(location:Location)
RETURN location.name


Get locations which are open on Saturdays

MATCH
  (:Saturday)<-[:validOn]-(:Periods)<-[:hasPeriods]-(:OpeningHours)<-[:hasOpeningHours]-(location:Location)
RETURN location.name


Get locations which are open at 11am

MATCH
  (period:Period)
WHERE
  period.from <= 1100 AND period.to > 1100
WITH
  period
MATCH
  (period)<-[:hasPeriod]-(:Periods)<-[:hasPeriods]-(:OpeningHours)<-[:hasOpeningHours]-(location:Location)
RETURN location.name


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment