Reputation: 23
I am following this Course Graph Analytics With Neo4j to explore Neo4j database. The database is very simple, which contains 11 nodes ('A', 'B','C'...), 14 relationships. I want to find all loops within this Neo4j database. I have used this following query to find the loops containing 'A'.
match p=(n)-[*]-(m)
where n.Name = ['A'] and m.Name = ['A']
return EXTRACT(n IN NODES(p)| n.Name) AS Paths, length(p)
order by length(p)
The problem is that, within several loops generated from the above query, some nodes has been visited twice, like 'C' in the following loop: ["A", "C", "D", "B", "C", "J", "F", "A"].
Which Filter function I can use to eliminate this. Thank you,
Upvotes: 2
Views: 3856
Reputation: 16375
I believe this query should work:
MATCH p=(n)-[*]-(m)
WHERE n.Name = ['A'] and m.Name = ['A']
AND NONE (node IN NODES(p) WHERE SIZE(
FILTER(x IN NODES(p) WHERE node = x AND x.name <> 'A')
) > 1
)
RETURN EXTRACT(n IN NODES(p)| n.Name) AS Paths, length(p)
order by length(p)
That is: the query uses the NONE(), SIZE() and FILTER() functions to guarantee that only paths without repeated nodes will be matched.
Based in this SO answer.
Upvotes: 1