Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the sort order lost when returning a collection via the REST API with resultDataContents=graph

Tags:

neo4j

cypher

For the following data:

create (a:Attendee {name:'luanne'});
create (a:Attendee {name:'adam'});
create (a:Attendee {name:'christoph'});
create (a:Attendee {name:'vince'});
create (a:Attendee {name:'michal'});

this query

MATCH p=(n:Attendee)-[r*0..1]-(m) 
WITH p order by head(nodes(p)).name 
return collect(distinct(p))

when executed via the shell return paths of length one, ordered by the name of the first node in the path.

However, via the REST API:

POST http://localhost:7474/db/data/transaction/commit
{"statements":[ 
{"statement":"MATCH p=(n:`Attendee`)-[r*0..1]-(m) WITH p order by head(nodes(p)).name return collect(distinct(p))","parameters":{},"resultDataContents":["graph"]}
]}]}

the same order is not maintained (looks like it's ordered by the node id).

How do I fix my Cypher to give me the same results as when executed via the shell?

PS. Works fine if resultDataContents is "row" and also when there is no COLLECT used in REST

like image 648
Luanne Avatar asked Nov 10 '22 13:11

Luanne


1 Answers

Collect is not needed in this case.

return distinct p should be enough.

In resultDataContents "graph" it reprocesses the response using sets to collect nodes and relationships for the response uniquely. Very likely that there the order is lost.

like image 128
Michael Hunger Avatar answered Dec 30 '22 11:12

Michael Hunger