Neo4j 如何使用密码计算节点之间的距离/跳数/长度(#关系)?

Neo4j 如何使用密码计算节点之间的距离/跳数/长度(#关系)?,neo4j,cypher,graph-databases,Neo4j,Cypher,Graph Databases,我试图确定图中节点之间的“距离”。使用示例电影数据库,我想返回距离Kevin Bacon的演员节点。使用下面的图片有效地了解我在寻找什么 如何将其构建到密码查询中?这似乎是可能的,我只是想不出一个方法来做这件事,因为我的cypher-foo还不是很先进:( 仅供参考-我的neo4j数据库是v4.0 此查询: MATCH p=(bacon:Person {name:"Kevin Bacon"})-[*1..5]-(hollywood) WHERE hollywood.name IN ['Helen

我试图确定图中节点之间的“距离”。使用示例电影数据库,我想返回距离Kevin Bacon的演员节点。使用下面的图片有效地了解我在寻找什么

如何将其构建到密码查询中?这似乎是可能的,我只是想不出一个方法来做这件事,因为我的cypher-foo还不是很先进:(

仅供参考-我的neo4j数据库是v4.0

此查询:

MATCH p=(bacon:Person {name:"Kevin Bacon"})-[*1..5]-(hollywood)
WHERE hollywood.name IN ['Helen Hunt', 'Ed Harris']
UNWIND
  REDUCE(s = [], i IN RANGE(0, LENGTH(p)) |
    s + {dist: i, node: NODES(p)[i]}
  ) AS data
RETURN data.dist AS distance, COLLECT(DISTINCT data.node) AS nodes
返回此结果:

╒══════════╤═════════════════════════════════════════════════════════════════════╕
│"distance"│"nodes"                                                              │
╞══════════╪═════════════════════════════════════════════════════════════════════╡
│0         │[{"name":"Kevin Bacon","born":1958}]                                 │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│1         │[{"title":"Apollo 13","tagline":"Houston, we have a problem.","releas│
│          │ed":1995},{"title":"A Few Good Men","tagline":"In the heart of the na│
│          │tion's capital, in a courthouse of the U.S. government, one man will │
│          │stop at nothing to keep his honor, and one will stop at nothing to fi│
│          │nd the truth.","released":1992},{"title":"Frost/Nixon","tagline":"400│
│          │ million people were waiting for the truth.","released":2008}]       │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│2         │[{"name":"Tom Hanks","born":1956},{"name":"Ed Harris","born":1950},{"│
│          │name":"Bill Paxton","born":1955},{"name":"Cuba Gooding Jr.","born":19│
│          │68},{"name":"Jack Nicholson","born":1937},{"name":"Ron Howard","born"│
│          │:1954}]                                                              │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│3         │[{"title":"Cast Away","tagline":"At the edge of the world, his journe│
│          │y begins.","released":2000},{"title":"Twister","tagline":"Don't Breat│
│          │he. Don't Look Back.","released":1996},{"title":"As Good as It Gets",│
│          │"tagline":"A comedy from the heart that goes for the throat.","releas│
│          │ed":1997},{"title":"Apollo 13","tagline":"Houston, we have a problem.│
│          │","released":1995}]                                                  │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│4         │[{"name":"Helen Hunt","born":1963},{"name":"Ed Harris","born":1950}] │
└──────────┴─────────────────────────────────────────────────────────────────────┘

请注意,电影“阿波罗13号”出现在距离为1和3的结果中。而“埃德·哈里斯”出现在距离为2和4的结果中。这是因为“凯文·培根”有不同长度的(无向)路径到这两个节点。

您的查询只查找Kevin Bacon和Helen Hunt或Ed Harris之间最多5步的路径,因此您的可视化不会显示与Kevin Bacon在给定“距离”处的“所有参与者节点”。您能否澄清您的实际用例是什么?另外,通常单个路径步骤被视为“距离”你似乎对“距离”有不同的定义,可能很难计算(或者其值可能非常模糊)--假设节点之间可以有任意数量的各种类型的关系,并且同一节点可能在同一路径中多次出现。@cybersam-是的,我在qry中将其限制为5个步骤,只是为了简化图像。我更新了图像,以更清楚地显示我的需要和更基本的距离定义。定义距离作为特定锚节点(Kevin Bacon)和图中其他节点之间的关系数。我实际上并不需要所有节点都使用它,只需要我的cypher查询中的节点,因此对于示例查询,只需添加一个附加属性“distance”,如上所述。这有意义吗?
╒══════════╤═════════════════════════════════════════════════════════════════════╕
│"distance"│"nodes"                                                              │
╞══════════╪═════════════════════════════════════════════════════════════════════╡
│0         │[{"name":"Kevin Bacon","born":1958}]                                 │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│1         │[{"title":"Apollo 13","tagline":"Houston, we have a problem.","releas│
│          │ed":1995},{"title":"A Few Good Men","tagline":"In the heart of the na│
│          │tion's capital, in a courthouse of the U.S. government, one man will │
│          │stop at nothing to keep his honor, and one will stop at nothing to fi│
│          │nd the truth.","released":1992},{"title":"Frost/Nixon","tagline":"400│
│          │ million people were waiting for the truth.","released":2008}]       │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│2         │[{"name":"Tom Hanks","born":1956},{"name":"Ed Harris","born":1950},{"│
│          │name":"Bill Paxton","born":1955},{"name":"Cuba Gooding Jr.","born":19│
│          │68},{"name":"Jack Nicholson","born":1937},{"name":"Ron Howard","born"│
│          │:1954}]                                                              │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│3         │[{"title":"Cast Away","tagline":"At the edge of the world, his journe│
│          │y begins.","released":2000},{"title":"Twister","tagline":"Don't Breat│
│          │he. Don't Look Back.","released":1996},{"title":"As Good as It Gets",│
│          │"tagline":"A comedy from the heart that goes for the throat.","releas│
│          │ed":1997},{"title":"Apollo 13","tagline":"Houston, we have a problem.│
│          │","released":1995}]                                                  │
├──────────┼─────────────────────────────────────────────────────────────────────┤
│4         │[{"name":"Helen Hunt","born":1963},{"name":"Ed Harris","born":1950}] │
└──────────┴─────────────────────────────────────────────────────────────────────┘