We are using neo4j version 5
I have a query:
match (m:Objectives {externalId:'13245'})-[rels1..10]->(n)
WHERE NONE(rel IN rels WHERE rel.edgeType = 8192)
unwind rels as rs RETURN m as base,COLLECT(DISTINCT (n)) AS nodes,
COLLECT(DISTINCT {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}) AS edges
This query works as expected. Gets all the nodes and relationships as expected.
Now we have additional requirement, where we want to get till it hits the n.status = 0. After that we do not want to get the remaining nodes and relations in that path.
Tried updating the query like:
match (m:%s {%s:%s})-[rels1..10]->(n)
WHERE NONE(rel IN rels WHERE rel.edgeType = 8192) and n.status <>0
unwind rels as rs RETURN m as base,COLLECT(DISTINCT (n)) AS nodes,
COLLECT(DISTINCT {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}) AS edges
This gives the results, but it only excludes the nodes with status =0, but rest of the disconnected nodes and relations are intact.
Tried using Apoc as follows:
MATCH (m:Objective {externalId:'123456'})
Match (end:Result) where end.status=0
With m, collect(end) as endNodes
CALL apoc.path.subgraphAll(m, {
relationshipFilter: “>”,
minLevel: 1,
maxLevel: 10,
endNodes: endNodes
})
YIELD nodes, relationships
RETURN nodes, relationships;
This returns only one level, stops getting the second level onwards. As it hits a node with status=0 in first level itself.
Please help to get proper query / apoc to get the results as expected here.
match path=(m:Objectives {externalId:'13245'})-[*..10]->(n)
WHERE
NONE(rel IN relationships(path) WHERE rel.edgeType = 8192) and
NONE(x in tail(nodes(path))[..-1] where x.status = 0)
unwind relationships(path) as rs
RETURN
m as base,
COLLECT(DISTINCT (n)) AS nodes,
COLLECT(DISTINCT {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}) AS edgese
BTW, I feel the following query will give the same results, but is less complicated:
match path=(m:Objectives {externalId:'13245'})-[*..10]->(n)
WHERE
(NOT EXISTS((n)-[]->()) OR length(path) = 10) and
NONE(rel IN relationships(path) WHERE rel.edgeType = 8192) and
NONE(x in tail(nodes(path))[..-1] where x.status = 0)
RETURN
m as base,
tail(nodes(path)) AS nodes,
[rs in relationships(path) | {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}] AS edges
Be aware that this may not scale well if your database start growing. You may like to start using QPP from V 5.9
It should be something like:
match path=(m:Objectives {externalId:'13245'})((a)-[r]->() WHERE r.edgeType <> 8192 AND a.status <> 0 ){0, 10}(n WHERE n.status = 0)
unwind relationships(path) as rs
RETURN
m as base,
COLLECT(DISTINCT (n)) AS nodes,
COLLECT(DISTINCT {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}) AS edgese
Thank you All. Here is the working solution .
match path=(m:Objective {externalId:'12345'})((a)-[r]->() WHERE r.edgeType <> 8192 AND (a.status is null or a.status =1) ){0, 10}(n WHERE n.status is null or n.status =1)
unwind relationships(path) as rs
RETURN
m as base,
COLLECT(DISTINCT (n)) AS nodes,
COLLECT(DISTINCT {uuid1: rs.uuid1, uuid2: rs.uuid2, edgeType: rs.edgeType}) AS edges