# Find paths with alternating relationships

**URL:** https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674
**Category:** Cypher
**Created:** [October 11, 2021, 9:41am UTC](https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674 "2021-10-11T09:41:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johanns.thomas\_neo4j](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/johanns.thomas_neo4j/32/4370_2.png) [@johanns.thomas\_neo4j](https://community.neo4j.com/u/johanns.thomas_neo4j)
#### Post date: [October 11, 2021, 9:41am UTC](https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674/1 "2021-10-11T09:41:23Z")

</div>

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").  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/4/34add2b431a4afad07bf67cd4c3195f4a3a0668d.png)  
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](https://stackoverflow.com/questions/20197360/cypher-query-finding-path-with-alternating-relationships), 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](https://community.neo4j.com/t/match-variable-length-paths-with-different-relationship-types/3739), @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.

---

<div class="post-metadata">

### Author: ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)
#### Post date: [October 11, 2021, 7:58pm UTC](https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674/2 "2021-10-11T19:58:35Z")

</div>

Hello,

For path expander sequences, the relevant documentation is here:

> **[Expand paths with config - APOC Extended Documentation](https://neo4j.com/labs/apoc/4.1/graph-querying/expand-paths-config/#path-expander-paths-config-config-sequences)**
>
> This section describes a procedure that can be used to expand the paths of variable length path traversals, while providing configuration options.

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.

---

<div class="post-metadata">

### Author: ![johanns.thomas\_neo4j](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/johanns.thomas_neo4j/32/4370_2.png) [@johanns.thomas\_neo4j](https://community.neo4j.com/u/johanns.thomas_neo4j)
#### Post date: [October 12, 2021, 3:22pm UTC](https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674/3 "2021-10-12T15:22:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)
#### Post date: [October 12, 2021, 9:13pm UTC](https://community.neo4j.com/t/find-paths-with-alternating-relationships/45674/4 "2021-10-12T21:13:08Z")

</div>

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.
