简体   繁体   中英

Neo4J Depth if there a node between 2 nodes

I have the following query:

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

What I'd like to return is the following:

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

But it returns also:

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

In the second part I tried to do:

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

But no success. Thanks for your help.

Your query has cardinality issue. If you want to match the second part optionally you can separate it and add an optional match clause to it. Following query should work as expected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM