根据路由中节点的最高值属性对neo4j中的路由进行排序

时间:2016-12-12 12:46:32

标签: neo4j

我在neo4j中有一个标准查询来搜索两个节点之间的所有最短路径。

MATCH 
     (object:Object { group: "12345" }), 
     (request:Request { group: "12345"}), 
     paths = allShortestPaths((request)-[*..12]->(object)) 
RETURN  
     paths

任务是按每条路线中节点的最高属性值对路线进行排序。

假设查询返回此类型的三个路由:

 [{prop:1},{prop:2},{prop:4}],
 [{prop:1},{prop:2},{prop:3}],    
 [{prop:1},{prop:2},{prop:5}],

任务是返回属性“prop”的最高值的路线,并按此顺序获取路线:

[{prop:1},{prop:2},{prop:3}],    
[{prop:1},{prop:2},{prop:4}],
[{prop:1},{prop:2},{prop:5}],

如果有人遇到,请就解决问题提出建议。

2 个答案:

答案 0 :(得分:1)

管理解决问题。这是一个查询。也许有人需要按相关性对路线进行排序。

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

答案 1 :(得分:1)

根据你的回答:

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

或者您可以使用来自apoc库的collection function max

apoc.coll.max([0.5,1,2.3])