简体   繁体   English

neo4j 中基于时间/日期的搜索

[英]Time/date based searches in neo4j

while playing around with neo4j a couple of questions arisen.在玩 Neo4j 时,出现了几个问题。 At present time there are 2 of them:目前有2个:

  • How can I limit the relationships/edges while searching for a specific path from node X to node Y (RegEx?/Wildcard?)?如何在搜索从节点 X 到节点 Y 的特定路径时限制关系/边(RegEx?/Wildcard?)? For instance all edges have 2 time attributes, "begin" and "end".例如,所有边都有 2 个时间属性,“开始”和“结束”。 I would like to find a path between nodes which occured between 2 am and 3 am in the morning.?我想找到发生在凌晨 2 点到 3 点之间的节点之间的路径。?
  • How can track a time path?如何跟踪时间路径? Lets say a car drove from 'A' to 'B'.假设一辆车从“A”开到“B”。 The route took one hour, namely from 5 am to 6 am.这条路线用了一个小时,即从早上5点到早上6点。 The next section, from 'B' to 'C' took as well one hour but from 7 am to 8 am because the driver took a one hour rest.下一部分,从“B”到“C”也用了一个小时,但从早上 7 点到早上 8 点,因为司机休息了一个小时。 How can I query neo4j in the way that only logically valid (time) paths are allowed.如何以仅允许逻辑有效(时间)路径的方式查询 neo4j。 For instance this should be valid:例如,这应该是有效的:

(Prague) -[:DISTANCE {begin: '5 am', end: '6 am'}]->(Brno), (Brno) -[:DISTANCE {begin: '7 am', end: '9 am'}]->(Liberec)

whereas this one shall be invalid:而这个应该是无效的:

(Prague) -[:DISTANCE {begin: '5 am', end: '6 am'}]->(Brno), (Brno) -[:DISTANCE {begin: '4 am', end: '6 am'}]->(Liberec)

Another example, given is the following graph:另一个例子,给出的是下图:

CREATE (a:BoxingMachine {title: 'A'})
CREATE (b:BoxingMachine {title: 'B'})
CREATE (c:BoxingMachine {title: 'C'})
CREATE (d:BoxingMachine {title: 'D'})
CREATE ((a)-[:FORWARDED {start_time:  '9:00 am'}]->(b))
CREATE ((a)-[:FORWARDED {start_time:  '7:00 am'}]->(c))
CREATE ((c)-[:FORWARDED {start_time:  '8:00 am'}]->(b))
CREATE ((a)-[:FORWARDED {start_time: '11:00 am'}]->(d))
CREATE ((d)-[:FORWARDED {start_time: '10:00 am'}]->(b))

I am looking for paths from node 'A' to node 'B'.我正在寻找从节点“A”到节点“B”的路径。 Ignoring the constraint "start_time", we have 3 possible paths in this graph:忽略约束“start_time”,我们在这个图中有 3 条可能的路径:

  • 'A' --> 'B'
  • 'A' --> 'C' --> 'B'
  • 'A' --> 'D' --> 'B'

In the case we consider the constraint "start_time" only, two paths left:在我们只考虑约束“start_time”的情况下,剩下两条路径:

  • 'A' --> 'B'
  • 'A' --> 'C' --> 'B'

because the path 'A' --> 'D' --> 'B' is invalid.因为路径'A' --> 'D' --> 'B'无效。 In this case the chronology is wrong.在这种情况下,年表是错误的。 Cooworker 'A' send a package to cooworker 'D' at 11am but cooworker forwarded this particular package to cooworker 'B' a hour earlier.同事“A”在上午 11 点将包裹发送给同事“D”,但同事提前一个小时将此特定包裹转发给了同事“B”。 Witch is impossible.女巫是不可能的。

PS: Is there a tool to generate the nodes with there edges as ASCII art resp. PS:是否有工具可以生成带有边缘的节点作为 ASCII 艺术。 for the console?对于控制台? :) :)

First, I would advise that you use a numeric time value instead of a string.首先,我建议您使用数字时间值而不是字符串。 It would make the Cypher a lot simpler and efficient.这将使 Cypher 变得更加简单和高效。 Also, practically speaking, paths can span multiple days, so just providing a time of day will lead to wrong results.此外,实际上,路径可以跨越多天,因此仅提供一天中的时间会导致错误的结果。 For instance, you may want to use epoch time .例如,您可能想要使用epoch time In my answer, I assume that the times are numeric.在我的回答中,我假设时间是数字。

This query should only return paths from A to B that are valid:此查询应仅返回从 A 到 B 的有效路径:

MATCH p=(a:BoxingMachine{title: 'A'})-[:FORWARDED*]->(b:BoxingMachine{title: 'B'})
WITH p, RELATIONSHIPS(p) AS rels
WHERE REDUCE(s = true, i IN RANGE(1, SIZE(rels)-1) |
  CASE WHEN (rels[i]).start_time > (rels[i-1]).start_time
  THEN s
  ELSE false END)
RETURN p;

The REDUCE clause is responsible for checking that the start times in a candidate path make sense. REDUCE子句负责检查候选路径中的开始时间是否有意义。

You can check the results using this data (with simple numeric time values, but epoch time would also work):您可以使用此数据检查结果(使用简单的数字时间值,但纪元时间也可以):

CREATE (a:BoxingMachine {title: 'A'}) 
CREATE (b:BoxingMachine {title: 'B'}) 
CREATE (c:BoxingMachine {title: 'C'}) 
CREATE (d:BoxingMachine {title: 'D'}) 
CREATE ((a)-[:FORWARDED {start_time:  9}]->(b)) 
CREATE ((a)-[:FORWARDED {start_time:  7}]->(c)) 
CREATE ((c)-[:FORWARDED {start_time:  8}]->(b)) 
CREATE ((a)-[:FORWARDED {start_time: 11}]->(d)) 
CREATE ((d)-[:FORWARDED {start_time: 10}]->(b))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM