Search for Polygon nodes using the found path nodes

Hi, I have a schema composed by WaterNodes, where they are connected between them via relationship CONNECTED. At each WaterNodes, there is a Shapefile node associated. I'm trying to get all Shapefiles of nodes that form a path, but the query seems to hang on the last Match clause. If I only return the elements it's OK, but when I try to use the last MATCH, doesn't work. BUT, if I use only match with list of id (roughly with the same size as the supposed ), the query works.

The code:

    MATCH p=(source:WaterNode)-[:CONNECTED*]->(:WaterNode{id:$id})-[:CONNECTED*]->(end:WaterNode)
    WHERE NOT (()-[:CONNECTED]->(source)) AND NOT ((end)-[:CONNECTED]->())
    WITH apoc.path.elements(p) AS elements
    UNWIND range(0, size(elements)-2) AS index
    WITH elements, index
    WHERE index %2 = 0
    MATCH (:WaterNode{id:elements[index].id})-[:REPRESENTED]->(f:GISFeature)
    RETURN apoc.convert.getJsonProperty(f, "geojson") AS geojson

I built a small example graph (50 nodes) with your node and relationship types and the query works for me. So there’s probably nothing inherently wrong about it, but your paths are just much longer.
Have you tried EXPLAIN and PROFILE to see what part of the query requires the most time? Maybe try the code below and see if it’s faster.

MATCH p=(source:WaterNode)-[:CONNECTED*]->(:WaterNode{id:$id})-[:CONNECTED*]->(end:WaterNode)
WHERE NOT (()-[:CONNECTED]->(source)) AND NOT ((end)-[:CONNECTED]->())
UNWIND nodes(p) as n
MATCH (n)-[:REPRESENTED]->(f:GISFeature)
RETURN apoc.convert.getJsonProperty(f, "geojson") AS geojson

Thank you very much for answering! The query seems to have hard work to match the GISFeature nodes and apoc.path.elements does more lag. Your query is simpler and The EXPLAIN proves that , but still hangs to get a result. The interesting fact is I only have 119 WaterNodes and 137 GISFeatures.

I tried a example where the number paths is 21 and the number of unique nodes are 67.

The first part of the query, gave the results in 17 ms:

MATCH p=(source:WaterNode)-[:CONNECTED*]->(:WaterNode{id:$id})-[:CONNECTED*]->(end:WaterNode)
WHERE NOT (()-[:CONNECTED]->(source)) AND NOT ((end)-[:CONNECTED]->())
UNWIND nodes(p) as n
RETURN n

but the last part that seems to be easy hangs for some reason. Maybe it's because indexing, I don't know. My metagraph is like this: https://ibb.co/DDxTxQb


LAST UPDATE
Seems that somehow the neo4j cannot reach to this GISFeature nodes that are tags as geometry. Maybe it's a error on upload query but if they don't exists why the result is not empty (hangs instead)? I realize that after seen the PROFILE where at the end the expected rows is zero. Than, graphically I search for those nodes and neo4j can't return the child nodes that correspond to these feature shapefiles

You're welcome!
How large is your JSON? Maybe the problem lies in the size of the JSON-string, like here: Neo4J browser graph render very slow due to property with large string