cypher foreach and complex queries

162 views
Skip to first unread message

Mat Tyndall

unread,
Nov 5, 2012, 6:09:31 PM11/5/12
to ne...@googlegroups.com
I'm having problems using foreach to create unique and set the new nodes.

Here's a simplified example of what I'm trying to do:

query:
START movie = node(0)  
FOREACH(a in {ACTORS} :
CREATE UNIQUE movie-[:CAST {characters: HEAD(a)}]->(actor HEAD(TAIL(a)))
SET actor.id = COALESCE(actor.id?, LAST(a)))

params = {
  ACTORS : [["Neo", {name: "Keanu"}, 1234]]
}

In the foreach statement I can do the create unique part just fine, but I also want to set an actor.id if it doesn't exist. The problem is that SET doesn't seem to recognize actor. Is this because multi part commands inside a foreach aren't allowed or is it something else? Splitting this into multiple foreach statements isn't practical because there's no good way to sync the actor.id paramater with the right node (maybe with a WHERE, but that'd be really messy).

Also, the foreach documentation is missing SET in it's list of approved commands.

Thanks,

-Mat

Mat Tyndall

unread,
Nov 7, 2012, 5:19:15 PM11/7/12
to ne...@googlegroups.com
Going to post this in issues.

Fails:
start n=node(*) foreach(m in [1,2,3] : 
                        create unique n-[:test]->(o {name: 'test' + STR(m)}) 
                        set o.id = COALESCE(o.id?,m)) 
match n-->m 
return m

Works:
start n=node(*) foreach(m in [1,2,3] : 
                        create unique n-[:test]->(o {name: 'test' + STR(m)})) 
match n-->m 
return m

Andres Taylor

unread,
Nov 7, 2012, 5:27:33 PM11/7/12
to ne...@googlegroups.com
Hi Mat,

I pushed a fix for this a couple of days ago. Have you tried this on a recent snapshot?

Andrés

--
 
 



--
The best way to ask for Cypher help: http://console.neo4j.org/usage.html 

chuwiey

unread,
Dec 2, 2012, 10:58:48 AM12/2/12
to ne...@googlegroups.com
Hi,

Related to this I think... > A query to add unique movies (by url) to the db

I tried:
START me=node(1), i=node:node_auto_index(type='item')
FOREACH (new_movie in {movies}:
    CREATE (new_movie)
    WHERE NOT(new_movie IN i.url))
RETURN new_movie

Running latest snapshot: Neo4j - Graph Database Kernel 1.9-SNAPSHOT (revision: 1.8.M07-147-g84d07a2)

Error: `)' expected but `W' found

Thanks

Michael Hunger

unread,
Dec 2, 2012, 11:13:17 AM12/2/12
to ne...@googlegroups.com
this is not a valid syntax:

FOREACH (new_movie in {movies}:
    CREATE (new_movie)
    WHERE NOT(new_movie IN i.url)

you have to filter movies instead:


FOREACH (new_movie in filter(m in {movies} WHERE NOT(new_movie IN i.url))
    CREATE (new_movie))

Michael

--
 
 

Reply all
Reply to author
Forward
0 new messages