Mahesha999
Mahesha999

Reputation: 24721

Remove smaller subset paths from the list of paths returned by the cypher

How can I remove smaller paths that are returned by the cypher which are already contained in other longer paths containted in the same result?

For example if my cypher is MATCH path=(n)-[:REL*]->(m) where id(n)=<some id> return path. Thus it returns all paths that contain any number of outgoing relationships of type [:REL]. However it returns multiple paths for a longer path (containing more than 2 nodes), each path containing path to the successive node in the longer path from the specified start node. For example, it may return following paths:

(a)-[:REL]->(b)
(a)-[:REL]->(b)-[:REL]->(c)
(a)-[:REL]->(b)-[:REL]->(c)-[:REL]->(f)
(a)-[:REL]->(b)-[:REL]->(d)
(a)-[:REL]->(c) 
(a)-[:REL]->(c)-[:REL]->(e)

As you can notice (a)-[:REL]->(b) is contained in all

(a)-[:REL]->(c) is contained in (a)-[:REL]->(c)-[:REL]->(e)

(a)-[:REL]->(b)-[:REL]->(c) is contained in (a)-[:REL]->(b)-[:REL]->(c)-[:REL]->(f)

I dont want (a)-[:REL]->(b), (a)-[:REL]->(c) and (a)-[:REL]->(b)-[:REL]->(c) to appear in the output as they are part of other paths.

Is this possible with cypher? Somehow I feel (though it feels weird to feel this), cypher should have allowed ! (logical not) before relationship specification to imply that the relationship of particular type does not go out of (or come in) the node. I could have written:

MATCH path=(n)-[:REL*]->(m)!-[:REL]->() where id(n)=<some id> return path

This would have disallowed those smaller paths.

Anyways how can I do above in cypher?

Upvotes: 0

Views: 240

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

You can add filtering for this, returning only paths where the end node does not have an outgoing :REL relationship:

MATCH path=(n)-[:REL*]->(m) 
where id(n)=<some id> and not (m)-[:REL]->()
return path

Upvotes: 1

Related Questions