Comparing relationship properties within a path

You want to compare relationship-properties of a path, either by a global value or parameter, or with each other within a path.

Basic model:

(:Person {person_id:'123'})-[:FRIEND {date:'1972-01-01'}]->(m:Person {person_id:'456'})

Make sure to have an constraint OR index on your nodes:

Constraint

CREATE CONSTRAINT ON (p:Person) ASSERT p.person_id IS UNIQUE

Index

CREATE INDEX ON :Person(person_id);

If you have your time in yyyy-mm-dd you can compare them directly: (with 2 digits aka 01) i.e. '2012-01-10' > '2011-08-31' )

WITH '1962-01-01' AS maxdate
MATCH (n:Person)-[f1:FRIEND]-()-[f2:FRIEND]-(m:Person)
WHERE n.person_id='180' AND f1.date < maxdate AND f2.date < maxdate
RETURN m;

You can also use the short form: (n:Person {person_id:'180'}):

If you want to have a general expression on relationships in a path, use a variable rels (which is then a collection) within your variable-length-path pattern:

WITH '1962-01-01' AS maxdate
MATCH (n:Person {person_id:'180'})-[rels:FRIEND*2]-(m:Person)
WHERE ALL(r IN rels WHERE r.date < maxdate)
RETURN m;

You can also use rels(path) function.

WITH '1962-01-01' AS maxdate
MATCH path = (n:Person {person_id:'180'})-[:FRIEND*2]-(m:Person)
WHERE ALL(r in rels(path) WHERE r.date < maxdate)
RETURN m;

Or if the relationships of a path are in relation to each other:

WITH '1962-01-01' AS maxdate
MATCH (n:Person  {person_id:'180'})-[rels:FRIEND*2]-(m:Person)
WHERE ALL(idx in range(0, size(rels)-2) WHERE (rels[idx]).date < maxdate AND (rels[idx]).date < (rels[idx+1]).date)
RETURN m;

Original question was on StackOverflow

What if I want to traverse different relation types and one of them must have a property set, like

MATCH (n:Company)-[:MAPPED_TO|:HAS_RESOURCE {Type:'Industry'}1..*2]->(r:Resource)

In my data Company-nodes have direkt HAS_RESOURCE-relations to Resource nodes or are MAPPED_TO Mapping-Nodes which then have HAS_RESOURCE-relations to Resource nodes. Both should be returned, but only if the Type-property on the HAS_RESOURCE-Node ist set to "Industry".

Is there any syntax that will work?

You should still be able to do a condition. E.g

WHERE all(r in relationships(path) 
          WHERE 
             CASE type(r) 
             WHEN 'HAS_RESOURCE' THEN r.Type='Industry' 
             ELSE true END)
1 Like