James Weaver
James Weaver

Reputation: 113

Excluding paths from Neo4j Cypher allShortestPaths() that contain a given node, or given relationship

What is the best way, given the following Neo4j Cypher query, for the returned collection of paths not to contain any path that contains an Item node whose itemId is equal to “Q5”?

MATCH p=allShortestPaths( (a:Item {itemId:"Q6294"})-[*]-(b:Item {itemId:"Q359442"}) ) 
RETURN p;

Also, what is the best way, given the above query, for the returned collection of paths not to contain any path that contains a relationship whose propId is equal to “P31”?

Thanks, James Weaver

Upvotes: 2

Views: 1202

Answers (1)

cybersam
cybersam

Reputation: 66989

  1. The returned collection of paths does not contain any path that contains an Item node whose itemId is equal to “Q5”:

    MATCH p=allShortestPaths( (a:Item {itemId:"Q6294"})-[*]-(b:Item {itemId:"Q359442"}) )
    WHERE NONE(x IN NODES(p) WHERE x:Item AND x.itemId = "Q5")
    RETURN p;
    
  2. The returned collection of paths does not contain any path that contains a relationship whose propId is equal to “P31”:

    MATCH p=allShortestPaths( (a:Item {itemId:"Q6294"})-[*]-(b:Item {itemId:"Q359442"}) )
    WHERE NONE(x IN RELATIONSHIPS(p) WHERE x.propId = "P31")
    RETURN p;
    

Upvotes: 7

Related Questions