Jeevanantham
Jeevanantham

Reputation: 1032

Neo4j - How to Ignore the pattern which has occurrence of same node more than once

How to Ignore the match patterns which has occurrence of same node more than once while query shortest path, example case in portrait in attached image. enter image description here

Shortest path of A TO C gives,

start a=node:node_auto_index(point='A'),c=node:node_auto_index(point='c') match p=a-[r:CONNECTS*]->c return p;

1. A-> C
2. A -> B -> C -> A -> C
3. A -> B -> C
4. A -> C -> A -> C
5. A -> B -> A -> C
6. A -> C -> B -> A -> B -> C

and more of 9 pattern, but in some pattern same node or start and end node are appeared more than once which will be like irrelevant output, so how can i identify and ignore the pattern which has any node's more than once in its path.

Upvotes: 0

Views: 239

Answers (2)

Jeevanantham
Jeevanantham

Reputation: 1032

found Solution for the above case

START a=node:node_auto_index(point='a'),
c=node:node_auto_index(point='c')
MATCH path= a-[r:CONNECTS*]->c
WHERE ALL(n in nodes(path) where 1=length(filter(m in nodes(path) : m=n))) 
RETURN path; 

Now its resulting only the valid paths as expected :-)

1. A -> B -> C
2. A -> C

Upvotes: 0

Michael Hunger
Michael Hunger

Reputation: 41706

You didn't use shortest path. The correct query is:

start a=node:node_auto_index(point='A'),
      c=node:node_auto_index(point='c') 
match p=shortestPath((a)-[r:CONNECTS*]->(c)) 
return p;

Or allShortestPaths

Upvotes: 1

Related Questions