In neo4j, how can I index by date and search in a date range. Also for times, I would like to search between 8am and 9am in a date range as well.
Indexes are commonly used for MATCH and OPTIONAL MATCH clauses that combine a label predicate with a property predicate.
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.
Note using APOC you can drop all indexes and constraints via CALL apoc. schema. assert({}, {}) . Nice - that's amazing and quick!
There's a convenient org.neo4j.index.lucene.LuceneTimeline which does this (using an integrated lucene index in neo4j).
Index the dates and times as integer timestamps. Then you can easily search in an index for dates between other timestamps. You can also index the time part of the timestamp separately as another integer, allowing you to query for specific times between given dates.
Example: The date and time to store is "2012-02-05 8:15 AM" So in your index, store "timestamp=1328447700" and "time=815"
Now you want to query the index for all events between 2012-02-01 and 2012-02-10 that occurred from 8:00 am to 9:00 am. You do that by querying the index for "timestamp>=1328072400 and timestamp<=1328936399 and time>=800 and time<=900"
The exact syntax for doing this depends on how you are connecting to Neo4j (REST or embedded) and which programming language you are using. But the idea is the same in any case.
This is an extension to Josh Adell's answer. For readability, I suggest having two date
and time
integer fields like
date:19970716 (YYYYMMDD)
time:203045000 (HHmmssuuu): last three digits for microseconds.
The int
datatype can store upto 2147483647
. If you are feeling adventurous, the long
datatype can store upto 9223372036854775807
.
http://docs.neo4j.org/chunked/stable/graphdb-neo4j-properties.html
Inspired from ISO 8601 timestamps like 1997-07-16T19:20:30.45Z
.
Disclaimer: I have only minimal experience with Neo4J.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With