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.

Apoc.do.when procedure for multiple conditions

Hi, I'm currently importing data from the Google Places API, which I then need to add to a graph. This data includes place id's, and place types (as a list), which may include cafe's restaurants, bars, among others. After importing, my Cypher looks like the following:

// import data from Google Places
MERGE (gp:Place {placeId: place_id})
CALL apoc.do.when('cafe' in types, 'SET gp:Cafe', '', {gp:gp}) YIELD value AS ignore1
CALL apoc.do.when('restaurant' in types, 'SET gp:Restaurant', '', {gp:gp}) YIELD value AS ignore2
CALL apoc.do.when('bar' in types, 'SET gp:Bar', '', {gp:gp}) YIELD value AS ignore3
RETURN count(*)

Many of the nodes I'm creating should return with labels of two or even three of the types, but I only ever end up with one 'type' label per node. Furthermore, if all of the passed Google place data include a type of 'cafe', and the cafe condition is tested first (as in the example above), then none of the successive procedures seem to be evaluated.

Any thoughts on why this is happening and possible ways it could be addressed?

3 REPLIES 3

HI Nathan,

Did you do a 'with' in between the CALL statements?
Another approach could be the old fashioned foreach 'hack' like this:

MERGE (gp:Place {placeId: place_id})
FOREACH (y in CASE WHEN 'cafe' IN types THEN [1] ELSE [] END | set gp:Cafe)
FOREACH (y in CASE WHEN 'restaurant' IN types THEN [1] ELSE [] END | set gp:Restaurant)
FOREACH (y in CASE WHEN 'bar' IN types THEN [1] ELSE [] END | set gp:Bar)
RETURN count(*)

regards

Kees

This 'FOREACH...' construct Kees suggested is well tested and works great.
IMHO it's time Neo4j gives it some syntactic sugar treatment so that it looks nice too.

Worked perfectly. Thanks!