Need to return relations type with apoc.spatial.sortByDistance

Hey, could you please please help me with a query.
I have my fancy graph with cities and relations types (like :AVIA, :TRAIN, etc). The initial query works fine, still, it does not return relations types. I've tried a bunch of variation, but due to lack of experience have no idea how to do it correctly.
Here is the query:
MATCH (a:City {name:'Kyiv'}), (b:City {name:'Lviv'})
MATCH p=(a)-->(b)
WITH collect(p) as paths
CALL apoc.spatial.sortByDistance(paths) YIELD path, distance
RETURN path, distance

│[{"name":"Kyiv","latitude":50.4500336,"longitude":30.5241361},{ !!!!!!HERE MUST BE A TYPE!!!!!!! },{"name":"Lviv","latitude":49.841952,"longitude":24.0315921} │467.37874514968337]

Hello @t.kenaz :slight_smile:

You can use type(r) to get the type of the r relation :slight_smile:

Doc: ( to see an example)


Thank you Maxime )))

It works if I do not set latitude and longitude, in other case the query returns error.
Variable r not defined (line 5, column 14 (offset: 180))
"RETURN type(r), path, distance"

The query was like this (and I've tried several variants):
MATCH (a:City {name:'Kyiv'}) -[r]-> (b:City {name:'Poltava'})
MATCH p=(a)-[]->(b)
WITH collect(p) as paths
CALL apoc.spatial.sortByDistance(paths) YIELD path, distance
RETURN type(r), path, distance

Still the problem is solved if I simply export a json -- it has all details included.

Try something (you mus specify r in the match and in the with clauses:

MATCH (a:City {name:'Kyiv'}) -[r]-> (b:City {name:'Poltava'})
MATCH p=(a)-[r]->(b)
WITH r, collect(p) as paths
CALL apoc.spatial.sortPathsByDistance(paths) YIELD path, distance
RETURN type(r), path, distance

Hey, thanks for the hint. Still sortPathsByDistance(paths) -- if I understand correctly -- is sortByDistance(paths) now. And it does not work, unfortunately. I tried several combinations, and I guess the issue is that of apoc, so I cannot return relations, and this is unreasonable... so I presume there is a mistake somewhere.

Hello @t.kenaz :slight_smile:

Have a look at RELATIONSHIPS(path) :slight_smile: You can use this function in comprehension list to get relations information :slight_smile: DOC

You can use it after the YIELD in a WITH clause before the return :slight_smile:


Thanks, will try ))))

1 Like

It is soooo frustrating, now I have mapping errors...
Cannot map (6)-[5:NEXT]->(0) to a nested result object for property path.
The query works in neo4j browser and in spring it is a disaster.
Here is what I've got:
"path": null,
"distance": 467.37874514968337
"path": null,
"distance": 919.730947031842
"path": null,
"distance": 1078.4521131704946
"path": null,
"distance": 1285.5076747122657
"path": null,
"distance": 1310.505104404419
...and in "evaluate" result of debugger I can see full details, but only if I use Object as a returning value.
Please if you have any idea how to populate those details from neo, pls help.
The result of Object in debugger (but not in json) are like this:
result = {ArrayList@8722} size = 5
0 = {RouteRepository$RouteData@8724}
"RouteRepository.RouteData(path=[(6)-[5:NEXT]->(0)], distance=467.37874514968337)"

path = {Object[1]@8760}
0 = {InternalPath$SelfContainedSegment@8761} "(6)-[5:NEXT]->(0)"

start = {InternalNode@8763} "node<6>"
 labels = {ArrayList@8769}  size = 1
 	 0 = "City"
 id = 6
 properties = {HashMap@8770}  size = 3
  	"name" -> {StringValue@8786} ""Kyiv""
  	"latitude" -> {FloatValue@8788} "50.4500336"
  	"longitude" -> {FloatValue@8790} “30.5241361"

relationship = {InternalRelationship@8764} "relationship<5>"
 start = 6
 end = 0
 type = "NEXT"
  	value = {byte[4]@8775} 
 	 coder = 0
  	hash = 0
 id = 5
 properties = {Collections$EmptyMap@8772}  size = 0

end = {InternalNode@8765} "node<0>"
 labels = {ArrayList@8773}  size = 1
 id = 0
 properties = {HashMap@8774}  size = 3

distance = 467.37874514968337

Hello @t.kenaz

Can you share the cypher query that is nor working? :slight_smile:


The query is the same, and it is perfectly fine. I just cannot populate the nested objects in java.
MATCH (a:City {name:$departure}), (b:City {name:$arrival})\n" +
"MATCH p=(a)-->(b)\n" +
"WITH collect(p) as paths\n" +
"CALL apoc.spatial.sortByDistance(paths) YIELD path, distance\n" +
"RETURN paths, distance LIMIT 5

I'm sorry, I never used Spring :confused:

Me neither ((( I just need it for this project. Thanks for the help anyway )))