How to filter path relationships by relationship properties?

Hi there,

My graph represents text with nodes as Tokens, with PRECEDES relationship to indicate the text ordering.

I have my Cypher query to return the subgraph of linked tokens that are reachable from a given root.

MATCH (to1:Token {content: "keep"})
OPTIONAL MATCH (to1)-[r:PRECEDES*..4]->(to2:Token)
RETURN (COLLECT(to1) + COLLECT(to2)) AS nodes, COLLECT(last(r)) AS rels

This goes to depth 4 and works OK. However, it doesn't work well for large data sets.

I want to use the relationship property occurrences to filter the subgraph that is returned. I'd like to keep only the top 10 relationships when expanding the above path, for every relationship r when sorted by r.occurrences. How could I do this? It might also be acceptable to be able to use a static condition r.occurrences > 10, although I'd prefer to be able to limit the relationships after sorting.

The simple attempt WHERE r.occurrences > 10 gives Type mismatch because r is a List<Relationship>, which makes sense I suppose.

I have looked at the APOC path expander procedures, but it seems that the relationshipFilter there doesn't support any conditions on relationship properties, it only supports filtering on relationship labels.


You would need to use predicate functions for this, and you would need a path variable so you could get the relationships from the path. Something like:

OPTIONAL MATCH path = (to1)-[r:PRECEDES*..4]->(to2:Token)
WHERE all(rel in relationships(path) WHERE rel.occurrences > 10)

As far as only expanding the top x relationships per node for each traversal down the path, that currently isn't possible with variable-length relationships, you would need to do that explicitly.