Neo4j Cypher: find the first matches in a linked list?

As in the question. The word "first" is important - there may be more relationships that match the same condition.

Real use case: each relation has a timestamp property, and I want to find the first thing that happened before a certain time (for example, "before noon"). For instance:.

(head) -[time: 9]-> () -[time: 8]-> () -[time: 7]-> ...

-

Here is what I had (suppose we know what headnode is):

MATCH (head) -[prevs:next*0..]-> (x) -[rel:next]-> (y)
WHERE NONE(prev IN prevs WHERE prev.time < {time})
AND rel.time < {time}
RETURN x, rel, y

This suggests that "cross one or more relationships until we find one in front {time}, And none of the previous relationships have been before {time}."

, , , . , - NONE() .

, Cypher, ? , ? (IOW, - " " ?)

-

:

http://console.neo4j.org/r/b4v2tl

: 1001- node, / . "Toggle Viz", .

, . :

MATCH (head:Node {id: 0}) -[prevs:next*0..]-> (x) -[rel:next]-> (y)
WHERE NONE(prev IN prevs WHERE prev.time > 5)
AND rel.time > 5
RETURN x, rel, y

, .

, Neo4j . 0.. , . 0..10, . 10, , . 100.

+3
3

:

MATCH (x)-[r:next]->(y) 
WHERE r.time > {time} 
RETURN x, r, y
ORDER BY r.time
LIMIT 1

,

START r=relationship:rels(time = {time})
MATCH (x)-[r1:next]->(y)-[r]->()
RETURN x,r1,y
0

I believe this was reviewed last year in Neo4j 3.0.3, as can be seen from this commit - https://github.com/neo4j/neo4j/commit/2c5c3dd

0
source

All Articles