Neo4j cypher preserves the order specified by the path for subsequent request

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 the results to be ordered by natural ordering, arising from the direction of the relationship -[:NEXT]->. But WITHin the third line takes this ordering. The problem is that I need to 1. filter for nodes :RELEVANTand 2. receive only different such nodes.

Is there a way to keep order? Maybe assign the order of numbers on the path and reuse it later with ORDER BY? I don’t know how to do it.

+4
source share
2 answers

, , node , , node.

DISTINCT min() ( max(), ) n. , 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;
+2

WHERE WITH :RELEVANT MATCH? , WHERE ... - :

MATCH p=((:Start)-[:NEXT*..100]->(n:RELEVANT))
WHERE ALL(node IN nodes(p) WHERE ...)
WITH DISTINCT n
...
RETURN n.someprop;
+1

Source: https://habr.com/ru/post/1682662/


All Articles