Reputation: 1082
I am using a query like
MATCH p=((:Start)-[:NEXT*..100]->(n))
WHERE ALL(node IN nodes(p) WHERE ...)
WITH DISTINCT n WHERE (n:RELEVANT)
...
RETURN n.someprop;
Where I want to have the results ordered by the natural ordering arising from the direction of the -[:NEXT]->
relationships.
But the WITH
in the third line scrambles up that ordering. Problem is, I need the with to 1. filter for :RELEVANT
nodes and 2. to get only distinct such nodes.
Is there some way to preserve the ordering? Maybe assign number ordering on the path and reuse it later with ORDER BY
? No idea how to do it.
Upvotes: 4
Views: 596
Reputation: 30397
You're asking for distinct nodes, which indicates that the node might be reachable by multiple paths, and thus might be present at multiple distances from the start node.
Instead of using DISTINCT, you should use min()
(or max()
, depending on your requirements) on the path length for each n
. Since those are aggregation functions, you will only ever get a single row for each n
.
MATCH p=((:Start)-[:NEXT*..100]->(n:RELEVANT))
WHERE ALL(node IN nodes(p) WHERE ...)
WITH n, min(length(p)) as distance
WITH n
ORDER BY distance
...
RETURN n.someprop;
Upvotes: 2
Reputation: 16355
And if you remove the WHERE
clause from WITH
and put the label :RELEVANT
in the MATCH
? Maybe the WHERE
is causing the problem... Try something this:
MATCH p=((:Start)-[:NEXT*..100]->(n:RELEVANT))
WHERE ALL(node IN nodes(p) WHERE ...)
WITH DISTINCT n
...
RETURN n.someprop;
Upvotes: 1