Find paths with alternating relationships

I'm trying to find paths with alternating relationships.
The use case is a product information manager.
Each product has attributes and a family.
There are rules.
Each rule has one or more conditions (e.g. attribute_1 = "foo" and family in ["bar"]) and one or more action (e.g. clear attribute_2, set attribute_3 to "foo").
What I want my query to find is: which rules might be trigger and which attributes might be modified if I modify a specific attribute.

I tried a suggestion from an old post on stackoverflow, it is indeed extremely slow; after 10 minutes I'm still waiting although there are only 200 rules, 120 attributes, 1272 TRIGGERS relationships and 210 MODIFIES relationships.

In this post, @andrew.bowman said "APOC path expander procedures have support for sequences of relationships, so if you just needed alternating :C and :D relationships it could support that"
I looked at the documentation but didn't understand enough to apply it to my use case.


For path expander sequences, the relevant documentation is here:

In particular, we define the sequence of relationships in the relationshipFilter config property passed to the path expander call. We use commas to separate the elements of the sequence. Within each element, we can use | to OR the accepted relationship types for that step of the sequence.

The example given uses this config property: relationshipFilter:'NEXT>,<FROM,POSTED>|REPLIED>'

Which means, from the starting node, we expand an outgoing :NEXT relationship, then an incoming :FROM relationship, then either an outgoing :POSTED relationship or an outgoing :REPLIED relationship, then the sequence repeats, traversing an outgoing :NEXT relationship and so on. As far as what to do with the nodes encountered in that sequence (whether to include or not, continue or not), that is handled in the labelFilter (and/or endNodes / terminatorNodes, if you have the references to those nodes already).

If your logic requires filtering based on node or relationship properties, then you may not be able to use the path expander, as it does not handle property-based filtering during expansion.

Looking to the future, there are changes that are planned that will allow native expression of repeating segments of an expansion, so when that time comes Cypher will be able to cover repeating sequences, and also property filtering during these expansions.

1 Like

This is (almost) perfect.
Is there a way to exclude specific relationships based on their property?
I tried blacklistRelationships but that didn't work.

Not based on property, unfortunately. You would need to perform filtering on the paths yielded from the call afterward, so that won't have the efficiency of handling it during expansion.