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

问题描述 投票:0回答:1

我正在尝试确定图中节点之间的“距离”。使用示例电影数据库,我想返回与凯文·培根(Kevin Bacon)相距遥远的演员节点。使用下面的图片可以有效地找到所需的内容。

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

MATCH p=(bacon:Person {name:"Kevin Bacon"})-[*1..5]-(hollywood)
WHERE hollywood.name in (['Helen Hunt', 'Ed Harris'])
RETURN p

FYI-我的neo4j数据库是v4.0

Example Distance metric

neo4j cypher graph-databases
1个回答
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}] │
└──────────┴─────────────────────────────────────────────────────────────────────┘

[请注意,电影“ Apollo 13”出现在结果中的距离为1和3。而“ Ed Harris”出现在结果中的距离为2和4。这是因为存在多条长度从(“凯文·培根”到这两个节点。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.