cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Optional Match pattern doesn't return value when partial pattern exist

venkat12
Node

I'm trying to find relationship count over a pattern / path. Where I need result even if the whole pattern doesn't exist (pattern exist partially)

 

MATCH (u:USER {first_name:'xyxy'})
OPTIONAL MATCH (u)<-[t1]-(:METADATA)-[t2]->(:USER)
RETURN count(distinct t1)

 

Query returns t1 count only if both t1 and t2 relationships exist in pattern.

But if t1 exit and t2 doesn't, then the t1 in the return gives me 0.

Is there any way to get result even if pattern partially exist. Any thoughts / suggestions?

1 REPLY 1

Cobra
Ninja
Ninja

Hello @venkat12 🙂

You could do something like this:

 

MATCH (u:USER {first_name:'xyxy'}) 
OPTIONAL MATCH (u)<-[t1]-(m:METADATA)
OPTIONAL MATCH (m)-[t2]->(:USER)
RETURN count(DISTINCT t1), count(DISTINCT t2)

 

Regards,
Cobra