在neo4j中,如何按日期索引并在日期范围内搜索?

在neo4j中,如何按日期索引并在日期范围内搜索。 有时候,我想在日期范围内的早上8点到9点之间搜索。

将日期和时间索引为整数时间戳。 然后,您可以轻松地在索引中搜索其他时间戳之间的日期。 您还可以将时间戳的时间部分分别索引为另一个整数,以便查询给定日期之间的特定时间。

示例:存储的日期和时间为“2012-02-05 8:15 AM”因此,在索引中,存储“timestamp = 1328447700”和“time = 815”

现在,您要查询2012-02-01和2012-02-10之间从上午8:00到上午9:00发生的所有事件的索引。 你可以通过查询索引“timestamp> = 1328072400和timestamp <= 1328936399,时间> = 800,时间<= 900”来做到这一点

执行此操作的确切语法取决于您如何连接到Neo4j(REST或嵌入式)以及您正在使用的编程语言。 但无论如何,这个想法都是一样的。

有一个方便的org.neo4j.index.lucene.LuceneTimeline可以做到这一点(在neo4j中使用集成的lucene索引)。

这是Josh Adell答案的延伸。 为了便于阅读,我建议有两个datetime整数字段,如

 date:19970716 (YYYYMMDD) time:203045000 (HHmmssuuu): last three digits for microseconds. 

int数据类型最多可以存储2147483647 。 如果您有冒险9223372036854775807long数据类型最多可存储9223372036854775807http://docs.neo4j.org/chunked/stable/graphdb-neo4j-properties.html


灵感来自ISO 8601时间戳,如1997-07-16T19:20:30.45Z

免责声明 :我对Neo4J的经验很少。

 with Spring data neo4j public List getAllEmailData(Date startDate, Date endDate) { List list = new ArrayList(); if (startDate == null || endDate == null) { return null; } long first = ConversionsUtils.convertDateToLong(startDate); long second = ConversionsUtils.convertDateToLong(endDate); try { list = emailRepository.searchAllData(first, second); // System.out.println("List size " +list.size()); } catch (Exception e) { e.printStackTrace(); } return list; } @Query( "START email=node:__types__(className='com.backend.core.neo.entities.Email') " + "WHERE email.searchDate > {0} and email.searchDate < {1}" + "RETURN email") List searchAllData(long startDate, long endDate); email entity @NodeEntity public class Email implements Serializable { private static final long serialVersionUID = 1L; public static final String CC = "CC"; public static final String TO = "TO"; @GraphId private Long id; @GraphProperty private Long senderId; @GraphProperty private String subject; @Indexed // @GraphProperty(propertyType = java.util.Date.class) private String dateSent; @Indexed private long searchDate; @GraphProperty private String emailTxt; @GraphProperty private String emailHtml; @GraphProperty private String emailId; //mail to @Fetch @RelatedTo(elementClass = User.class, type = TO, direction = Direction.OUTGOING) private Set intoUsers; //mail shared @Fetch @RelatedTo(elementClass = User.class, type = CC, direction = Direction.OUTGOING) private Set sharedUsers;