Optimize cypher - match union where clause

16 views
Skip to first unread message

Stefaan Delanghe

unread,
Oct 16, 2016, 7:14:22 PM10/16/16
to Neo4j
I have a cypher which is build dynamically as opposed to call separate cyphers all the time. This is an optimization for performance.

This is the cypher i run. the match union concatenation is dynamically.
My issue is that i would like to retrieve those line that do not match 

MATCH (c:cc {type : 'type'}) WHERE c.email is not null RETURN c.type as personType, 'email' as attribute, c.email as measure , count(c) as attributeCount 
UNION 
MATCH (c:cc {type : 'type'}) WHERE c.address is not null RETURN c.type as personType, 'address' as attribute, c.address as measure , count(c) as attributeCount 
UNION 
MATCH (c:cc {type : 'type'}) WHERE c.phone is not null RETURN c.type as personType, 'phone' as attribute, c.phone as measure , count(c) as attributeCount 
UNION 
MATCH (c:cc {type : 'type'}) WHERE c.social is not null RETURN c.type as personType, 'social' as attribute, c.social as measure , count(c) as attributeCount 
UNION 
MATCH (c:cc {type : 'type'}) WHERE c.twitter is not null RETURN c.type as personType, 'twitter' as attribute, c.twitter as measure , count(c) as attributeCount 
UNION 
MATCH (c:cc {type : 'type'}) WHERE c.facebook is not null RETURN c.type as personType, 'facebook' as attribute, c.facebook as measure , count(c) as attributeCount


This will give back something like this.
"type" , 'email' , 1, 34
"type" , 'phone' , 1, 21

But i want that even when some are not found that a row is given back with 0. I would like a list like this
"type" , 'email' , 1, 34
"type" , 'phone' , 1, 21
"type", "address", 0, 0
"type", "social", 0, 0

Many thanks for your possible advise




Michael Hunger

unread,
Oct 16, 2016, 7:53:39 PM10/16/16
to ne...@googlegroups.com
You would need to use optional match then.

Would it make sense to turn 'type' into a label too?

you could rewrite your query into:

MATCH (c:cc {type : 'type'})
UNWIND keys(c) as key
WHERE key IN ['facebook','email','phone',...]
RETURN c.type as personType, key as attribute, c[key] as measure , count(c) as attributeCount

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages