WHERE clause with Variable Length Paths

Hello everyone,

I have a set of nodes (companies) which are related to each other (transactions). For some of those companies (with a label "Scope"), I'm interested in getting their neighborhood of order N. However I would like to identify all outgoing transactions where a specific property verifies a certain condition.

So far, I'm using Variable Length Path to get the neighborhood, but then I would like to apply a WHERE clause to identify which relationships verify the condition to be able to mark them (SET a new property with value 1 for example).

The Cypher query I had in mind would be something like that :

MATCH p = (startCompany)-[r:PAID*1..N]->(endCompany)
WHERE startCompany:Scope
AND ... (edges with condition verified)
SET ... (mark those edges to identify them)

However, I'm not sure how WHERE behaves with paths that have multiple relationsiphs in them : does it filter only if all relationships verify the condition ? Or at least one ?

If someone could help me figure this out (and also has an idea of what the query should look like), it would be great !

I read this first blog article called The Power of the Path – Part 1 which is very interesting, but unfortunately the author didn't add any other parts after that.

Thank you !

Something like this will work (I've removed unused variables):

MATCH (:Scope)-[r:PAID*1..N]->()
UNWIND [rel IN r WHERE rel.p = 41] AS rel
SET rel.q = 1

In this example, relationships with property p = 41 have property q updated to 1.

Thank you, it works very well !

So if I understand it correctly, WHERE clause can only be applied on a single relationship ?

A where clause can have any predicate. If you want to apply a condition on all the relationships at once, or any of them, you should look at the predicate functions.

Your use case was to perform an operation on those relationships that met a condition. As such, you need to look at each individually.

Sometimes you may want to have a condition across a collection of items, such as, relationships along a path or nodes in a list. This is when the predicate functions are useful.

Another useful cypher clause is the forEach. It could have been used in @finbar.good ’s solution as an alternative to the unwind.

1 Like