Reputation: 53
I have a standard query in neo4j to search for all shortest routes between two nodes.
MATCH
(object:Object { group: "12345" }),
(request:Request { group: "12345"}),
paths = allShortestPaths((request)-[*..12]->(object))
RETURN
paths
The task is to sort the routes by the highest property value of the nodes in each route..
Let's say the query returns three routes of this type:
[{prop:1},{prop:2},{prop:4}],
[{prop:1},{prop:2},{prop:3}],
[{prop:1},{prop:2},{prop:5}],
The task is to return the routes for the highest value of the property "prop" and get the routes in this order:
[{prop:1},{prop:2},{prop:3}],
[{prop:1},{prop:2},{prop:4}],
[{prop:1},{prop:2},{prop:5}],
If anyone faced, please give advice on solving the problem.
Upvotes: 2
Views: 117
Reputation: 29172
Based on your answer:
MATCH
(object:Object { group: "12345" }),
(request:Request { group: "12345"}),
paths = allShortestPaths((request)-[*..12]->(object))
WITH
paths, EXTRACT(x IN nodes(paths) | toInt(x.prop)) AS propsSort
RETURN
paths, REDUCE(m=propsSort[0],
v in propsSort[1..] |
CASE WHEN v>m THEN v ELSE m END
) as sort
ORDER BY
sort DESC
Or you can use the collection function max
from apoc library:
apoc.coll.max([0.5,1,2.3])
Upvotes: 1
Reputation: 53
Managed to solve the problem. Here is a query. Maybe someone will need to sort the routes by relevance.
MATCH
(object:Object { group: "12345" }),
(request:Request { group: "12345"}),
paths = allShortestPaths((request)-[*..12]->(object))
WITH
paths, EXTRACT(x IN nodes(paths) | toInt(x.prop)) AS propsSort
UNWIND
propsSort as sort
RETURN
paths, max(sort) as sort
ORDER BY
sort DESC
Upvotes: 1