• neo4j查询两节点之间所有路径/关系


    目录

    数据源

    查询两节点有效的路径

    所有路径(不准确)

    有效路径(准确)

    查询两节点之间最短路径

    查询两节点之间所有的最短路径


    数据源

    查询两节点有效的路径

    路径长度(5个节点之内),一般都会做限制,否则数据库一但内容过多就会卡死

    所有路径(不准确)

    最容易想到的就是这样写,但是结果是不准确的。

    1. match p = (a)-[r*..5]-(b)
    2. where a.name = '刘备' and b.name='刘禅'
    3. return p

    为什么不准确?

    可以看到,刘禅与关羽、张飞、糜夫人、糜芳、糜竺  关系不是很大,因为他们是刘备的关系群

    那为何还是能搜到呢?

    因为我们设置了最大路径是5

    因此张飞出现的原因就是这样的路径

    1. 刘备-张飞-刘备-刘禅
    2. 刘备-张飞-关羽-刘备-刘禅

    可以看到,刘备(目标节点)在一条路径中出现了两次,所以导致一些不相关的节点出现了,其他节点(糜夫人、糜芳、糜竺)也是类似问题。

    问题验证

    我们可以把每条路径中的节点名称抽取出来看看

    1. match p = (a)-[r*..5]-(b)
    2. where a.name = '刘备' and b.name='刘禅'
    3. return extract(n in nodes(p)| n.name)

    与我们想的结果一致

    如何解决这个问题?

    问题就是在一条路径(p)中,有重复人员的出现

    那么解决方案就是,去掉路径(p)中有重复人员的路径(p)

    1. // 相当于遍历nodes, 每得到一个node就在nodes中找一下他自己有几个,如果不等于1就不要了
    2. and ALL( n1 in nodes(p) where size(filter(n2 in nodes(p) where id(n1) = id(n2)))=1 )
    3. // 新版本neo4j 不支持fliter函数 用[]代替
    4. and ALL( n1 in nodes(p) where size([n2 in nodes(p) where id(n1) = id(n2)])=1 )

    有效路径(准确)

    最终方案

    1. match p = (a)-[r*..5]-(b)
    2. where a.name = '刘备' and b.name='刘禅'
    3. and ALL( n1 in nodes(p) where size(filter(n2 in nodes(p) where id(n1) = id(n2)))=1 )
    4. return p
    5. // 新版
    6. match p = (a)-[r*..5]-(b)
    7. where a.name = '刘备' and b.name='刘禅'
    8. and ALL( n1 in nodes(p) where size([n2 in nodes(p) where id(n1) = id(n2)])=1 )
    9. return p

    查询两节点之间最短路径

    // TODO 还可以设置最短路径权重

    1. match p = shortestpath((a)-[r*0..4]-(b))
    2. where a.name = '刘备' and b.name='刘禅'
    3. return p

    查询两节点之间所有的最短路径

    最短路径如果不加权重的话,不如用所有最短路径,因为你没有规定最短路径的含义。

    这个也经常用,但是如果梳理两个节点关系的时候 这样写会漏掉长的路径,导致结果不完全。

    1. match p = allshortestpaths((a)-[r*0..4]-(b))
    2. where a.name = '刘备' and b.name='刘禅'
    3. return p

  • 相关阅读:
    12.6 - 每日一题 - 408
    Vue 3 总结
    低代码开源项目整理
    vue使用谷歌地图实现地点查询
    DHCP原理与配置
    微服务架构中实体类模块化设计与MyBatis-Plus注解浅析
    w字符编码
    java毕业设计菜篮子系统mybatis+源码+调试部署+系统+数据库+lw
    TensorFlow实战教程(十七)-Keras搭建分类神经网络及MNIST数字图像案例分析
    【Linux】Shell使用sh和bash区别
  • 原文地址:https://blog.csdn.net/java_creatMylief/article/details/127664455