Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In neo4j, how can I index by date and search in a date range?

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.

like image 651
Phil Avatar asked Feb 04 '12 06:02

Phil


People also ask

What do Indexes do in Neo4j?

Indexes are commonly used for MATCH and OPTIONAL MATCH clauses that combine a label predicate with a property predicate.

Does Neo4j support datetime?

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.

How do I delete all constraints in Neo4j?

Note using APOC you can drop all indexes and constraints via CALL apoc. schema. assert({}, {}) . Nice - that's amazing and quick!


3 Answers

There's a convenient org.neo4j.index.lucene.LuceneTimeline which does this (using an integrated lucene index in neo4j).

like image 42
Mattias Finné Avatar answered Oct 07 '22 11:10

Mattias Finné


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.

like image 133
Josh Adell Avatar answered Oct 07 '22 12:10

Josh Adell


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.

like image 23
Jesvin Jose Avatar answered Oct 07 '22 10:10

Jesvin Jose