使用Java代码实现获取Neo4j图谱的最短路径。
1.cypher查询
MATCH (n1:GraphNode { id: '16560' }),(n2:GraphNode { id: '12312' }), p = shortestPath((n1)-[*]-(n2))
WHERE length(p)> 1
RETURN p
2.Java实现
@Component
public interface GraphNodeRepository extends Neo4jRepository<GraphNode,String> {
@Query("MATCH (n1:GraphNode { id: {sourceId} }),(n2:GraphNode { id: {targetId} }), p = shortestPath((n1)-[*]->(n2))\n" +
"WHERE length(p)> 1\n" +
"RETURN p")
List<GraphNode> calculateShortestPath(@Param("sourceId") String sourceId,@Param("targetId") String targetId);
}
References:
- Neo4J Java API – Shortest Path for specific node label or excluding a particular relationship;
- neo4j-documentation:CalculateShortestPath.java;
- Graph algorithm examples;
- Shortest path planning;
最新评论
本来在正常下载,突然就不能下载,并出现报错:a socket operation was attempted to an unreachable network。请问是什么原因呢
试了,还是不行,能不能更新一下
试了,确实不行,能不能更新一下
也推荐一下我自己写的 https://twitdown.com