Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-12-2022 11:26 AM
I have a query that is returning more paths than it needs to because it will go from the start node to other nodes, then back through the start node again
Here are some of the paths it returns:
CREATE (n3:Test {appId: 3})<-[:MEDIUM]-(n0:START:Test {appId: 1})-[:WEAK]->(:Test {appId: 2})-[:STRONG]->(n3),
(n0)-[:STRONG]->(:Test {appId: 4})-[:STRONG]->(n4:Test {appId: 5})-[:WEAK]->(:Test {appId: 6})-[:MEDIUM]->(:Test {appId: 8}),
(n4)-[:MEDIUM]->(n11:Test {appId: 7})-[:STRONG]->(:Test {appId: 9}),
(n11)-[:WEAK]->(:Test {appId: 10})
MATCH(n:Test {appId: 1})
MATCH p=(n)-[*]-(m)
WHERE NOT m.appId = 1
WITH m, relationships(p) AS rels
WITH m,
any(r IN rels WHERE type(r) = 'MEDIUM') AS medium,
any(r IN rels WHERE type(r) = 'WEAK') AS weak
WITH m, CASE
WHEN weak THEN "WEAK"
WHEN medium AND NOT weak THEN "MEDIUM"
WHEN NOT medium AND NOT weak THEN "STRONG"
END AS weight
RETURN collect(m.appId), weight
11-12-2022 03:24 PM
Try this. It selects only paths where the path nodes includes the 'n' node only once. Note, I also added the 'distinct' clause to your collect, as the list had duplicates.
MATCH(n:Test {appId: 1})
MATCH p=(n)-[*]-(m)
WHERE single(i in nodes(p) where id(i) = id(n))
WITH m, relationships(p) AS rels
WITH m,
any(r IN rels WHERE type(r) = 'MEDIUM') AS medium,
any(r IN rels WHERE type(r) = 'WEAK') AS weak
WITH m, CASE
WHEN weak THEN "WEAK"
WHEN medium AND NOT weak THEN "MEDIUM"
WHEN NOT medium AND NOT weak THEN "STRONG"
END AS weight
RETURN collect(distinct m.appId), weight
All the sessions of the conference are now available online