0

How to express a conditions for two consecutive variable length relationships?

Consider this partial query

MATCH(t1:Type{myID: 1})-[r:relType]->(:Type)-[rels:relType*0..]-(t2:Type{myID:100})
WHERE r.attr1>10

Basically I am trying to saying that there could be one or more relations from t1 to t2. The first relation r should satisfy a given condition on its attribute.

If this is the only relation between the two nodes then it's ok.

It at least another relation exist I want to add another condition such as:

WHERE  r.attr1>10 AND r_next.attr2> r_prev.attr2+r_prev.attr1

where r_next and r_prev are consecutive relations: ()-[r_prev]->()-[r_next]-(). Note that at the first step r_prev is the first relation r.

I know rels is a collection but I do not know how to express such a condition.

roschach
  • 8,390
  • 14
  • 74
  • 124

1 Answers1

0

Consecutive comparison like this isn't easy at this time, and it can't currently be evaluated during expansion.

You can do some filtering on this after, but it will be ugly.

We'll make use of the APOC Procedures for apoc.coll.pairsMin(), which takes a collection and returns a list of adjacent pairs.

MATCH (t1:Type{myID: 1}), (t2:Type{myID:100})
MATCH (t1)-[r:relType]->(:Type)-[rels:relType*0..]-(t2)
WHERE r.attr1>10
WITH t1, t2, apoc.coll.pairsMin(rels) as pairs
WHERE all(pair in pairs WHERE pair[0].attr1 + pair[0].attr2 < pair[1].attr2)
RETURN t1, t2 //or whatever you want to return from this
InverseFalcon
  • 29,576
  • 4
  • 38
  • 51
  • neither with the `p=shortestPath((t1)-[:TripEdge*]->(b2) WHERE ALL (r IN relationships(p) WHERE ...)`? – roschach Jul 25 '19 at 16:18