首页 > 解决方案 > 如果 2 个节点之间有一个节点,则 Neo4J 深度

问题描述

我有以下查询:

MATCH
   (exp:Expert {name: "Somebody"})-[:PUBLISHED_BY]-(pub1:Publication)-[:PUBLISHED_BY]-(coexp:Expert),
   (coexp:Expert)-[:PUBLISHED_BY]-(pub2:Publication)-[:PUBLISHED_BY]-(cocoexp:Expert)
RETURN exp,pub1,pub2,coexp,cocoexp
LIMIT 300

我想返回的是以下内容:

(expert)--(publication)--(coexpert)
(expert)--(publication)--(coexpert)--(publication)--(cocoexpert)

但它也返回:

(expert)--(publication)--(coexpert)--(publication)--(cocoexpert)--(publication)--(cococoexpert)
...

在第二部分中,我尝试做:

(coexp:Expert)-[:PUBLISHED_BY]-(pub2:Publication)-[:PUBLISHED_BY*0..1]-(cocoexp:Expert)

但没有成功。谢谢你的帮助。

标签: neo4j

解决方案


您的查询存在基数问题。如果您想选择性地匹配第二部分,您可以将其分开并添加一个可选的匹配子句。以下查询应按预期工作:

MATCH (exp:Expert {name: "Somebody"})-[:PUBLISHED_BY]-(pub1:Publication)-[:PUBLISHED_BY]-(coexp:Expert)
OPTIONAL MATCH (coexp)-[:PUBLISHED_BY]-(pub2:Publication)-[:PUBLISHED_BY]-(cocoexp:Expert)
Where exp<>coexp And exp<>cocoexp And coexp<>cocoexp
RETURN exp,pub1,pub2,coexp,cocoexp
LIMIT 300

推荐阅读