rarry
rarry

Reputation: 3573

Query Neo4j for many date ranges

I am using neo4j database. For each relationship, I keep a pair of dates in properties that spacify when the relationship holds e.g.

{
  "identity": 1,
  "type": "HAS_NAME",
  "properties": {
      "start": 1989,
      "end": 1990
  }
}

which means, that the relationship was true between 1989 and 1990. Now, I would like to extend properties to have many pairs of dates e.g.

{
  "identity": 1,
  "type": "HAS_NAME",
  "properties": {
      "start1": 1989,
      "end1": 1990,
      "start2": 2001,
      "end2": 2002,
      "start3": 2010,
      "end3": 2015,
      ...
  }
}

which means, that the relationship was true between 1989 and 1990 and also between 2001 and 2002 and between 2010 and 2015. I would like to be able to query for relationships by dates, e.g. "find all relationships that were true between queryStart and queryEnd".

In the above example, I could write

WHERE (queryEnd > start1 AND queryStart < end1) 
OR (queryEnd > start2 AND queryStart < end2) 
OR (queryEnd > start3 AND queryStart < end3)

However, I do not know how many pairs the relationship will have. Is there a standard approach to achieve that in neo4j? I do not have to keep those dates in properties.

Upvotes: 0

Views: 243

Answers (1)

Graphileon
Graphileon

Reputation: 5385

Since you can have multiple relationships of the same type between any two given nodes, you can create a :HAS_NAME relationship for each period. And just match using (assuming you provide queryStart and queryEnd as parameters)

MATCH (n)-[r:HAS_NAMe]->(m)
WHERE ($queryEnd > r.start AND $queryStart < r.end) 

to retrieve the relationships.

Upvotes: 1

Related Questions