[英]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个:
(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.