# How to optionally extract a part of the result path if a curtain node came on the path in cypher query result?

**URL:** <https://community.neo4j.com/t/how-to-optionally-extract-a-part-of-the-result-path-if-a-curtain-node-came-on-the-path-in-cypher-query-result/11970>\
**Category:** Cypher\
**Created:** [November 18, 2019, 7:38am UTC](https://community.neo4j.com/t/how-to-optionally-extract-a-part-of-the-result-path-if-a-curtain-node-came-on-the-path-in-cypher-query-result/11970 "2019-11-18T07:38:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sura-nsgindia](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sura-nsgindia/32/7368_2.png) [@sura-nsgindia](https://community.neo4j.com/u/sura-nsgindia)\
**Post date:** [November 18, 2019, 7:38am UTC](https://community.neo4j.com/t/how-to-optionally-extract-a-part-of-the-result-path-if-a-curtain-node-came-on-the-path-in-cypher-query-result/11970/1 "2019-11-18T07:38:29Z")

</div>

For example:

Situation 1:

(a) - [:CONNECTED] -\> (z)

result 1:  
(a) - (b) - (c) - (d) - (stop) - (e) - (f) - (z)

Now as the stop node came between the result, I want the following result:  
(a) - (b) - (c) - (d) - (stop)

Situation 2:

(a) - [:CONNECTED] -\> (x)

result:  
(a) - (b) - (c) - (d) - (e) - (f) - (x)

Now as the stop node is not present between the result, I want the whole result:  
(a) - (b) - (c) - (d) - (e) - (f) - (x)

Can one one help me on this?

Thank you

---

<div class="post-metadata">

**Author:** ![daveb](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/daveb/32/7614_2.png) [@daveb](https://community.neo4j.com/u/daveb)\
**Post date:** [November 26, 2019, 11:30pm UTC](https://community.neo4j.com/t/how-to-optionally-extract-a-part-of-the-result-path-if-a-curtain-node-came-on-the-path-in-cypher-query-result/11970/2 "2019-11-26T23:30:31Z")

</div>

Here are a few queries to get you going on your solution.

This is the simplest one. Look for a path, p, that starts at 'A' and ends at a node with either 'Z' or 'stop'. Then return only the shortest of all of the matches if there are multiple. The theory here is that if the 'stop' is encountered first it will stop there.

```auto
MATCH p=(a:Node {name: 'A'}) - [:CONNECTED*] -> (end:Node)
WHERE end.name in ['stop','Z']
RETURN p
ORDER BY length(p)
LIMIT 1

```

Here is a another pure cypher example. This one finds the path from A..Z first and then looks for a stop node afterwards. If there is/are stop node(s) it collects them and then uses the first one to only return from the beginning on the path through the stop node.

```auto
MATCH p=(a:Node {name: 'A'})-[:CONNECTED*]->(z:Node {name: 'Z'})
UNWIND range(1,length(p)) AS idx
WITH p, collect(CASE WHEN (nodes(p)[idx-1]).name = 'stop' THEN idx END) AS stops_found
RETURN nodes(p)[0..stops_found[0]]

```

Here is an APOC example. This example uses `apoc.path.expand`. It findsthe A and Z nodes first and then it finds a collection of stopper nodes. It then passes these into apoc.path.expandConfig and follows `CONNECTED` relationships and `Node` labels until it reaches either a stopper node or Z.

```auto
MATCH (a:Node {name: 'A'}), (z:Node {name: 'Z'})
WITH a, z
MATCH (stop:Node {name: 'stop'}) 
WITH a, z, collect(stop) AS stoppers
CALL apoc.path.expandConfig(a, {relationshipFilter:'CONNECTED>', labelFilter:'+Node', terminatorNodes: stoppers + [z], uniqueness: 'NODE_GLOBAL'}) YIELD path
RETURN path

```

Here is another APOC example. This is similar, but instead of "stop" being an attribute value on a node it uses a termination filter on a `Stop` label. It does not look for the end node Z but will continue on matching connections provided no `Stop` label is encountered.

```auto
MATCH (a:Node {name: 'A'})
CALL apoc.path.expandConfig(a, {relationshipFilter:'CONNECTED>', labelFilter:'+Node|/Stop', uniqueness: 'NODE_GLOBAL'}) YIELD path
RETURN path

```

---

<div class="post-metadata">

**Author:** ![sura-nsgindia](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sura-nsgindia/32/7368_2.png) [@sura-nsgindia](https://community.neo4j.com/u/sura-nsgindia)\
**Post date:** [November 27, 2019, 5:08am UTC](https://community.neo4j.com/t/how-to-optionally-extract-a-part-of-the-result-path-if-a-curtain-node-came-on-the-path-in-cypher-query-result/11970/3 "2019-11-27T05:08:22Z")

</div>

Thank you daved for the solution and for considering different use-cases.
