Cypher conditional create and return values.

1,589 views
Skip to first unread message

Aran Mulholland

unread,
Jun 30, 2013, 8:31:27 AM6/30/13
to ne...@googlegroups.com
I have a couple of nodes. A user and a post. I want to add a like relationship between the user and the post (user-[LIKE]->post) only if the like does not already exist. This query does the trick

START u=node:node_auto_index(UserIdentifier = 'USER12'), p=node:node_auto_index(PostIdentifier = 'USER5POST4')
MATCH u-[existingLike?:LIKE]->p
WHERE existingLike IS NULL
CREATE u-[l:LIKE]->p
RETURN u, l, p;

In my service call I want to know if the user exists, the post exists and if the new like has been added. This part works only OK when the new like is added as I get (u, l and p) return values. When there is an existing like or the user or post is missing I get no return values.

Is there a way to return u and p using only one Cypher query whether or not the create statement executed?

Peter Neubauer

unread,
Jun 30, 2013, 9:30:57 AM6/30/13
to Neo4j User


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Kids in Malmö this summer?        - http://www.kidscraft.se
Neo4j questions? Use GraphGist. - http://gist.neo4j.org


--
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Hunger

unread,
Jun 30, 2013, 10:05:36 AM6/30/13
to ne...@googlegroups.com
For neo4j 1.9 see 

create unique

Sent from mobile device
--

Aran Mulholland

unread,
Jun 30, 2013, 5:06:29 PM6/30/13
to ne...@googlegroups.com
Thanks Peter, I'll check out merge. 
Michael if the relationship has a time stamp property (which on further thought I'd like to add) then CreatUnique will create a new one every time. Is there anyway to use CreateUnique in this scenario?

Sent from my iPhone

Wes Freeman

unread,
Jun 30, 2013, 6:49:31 PM6/30/13
to ne...@googlegroups.com
Like this (assuming you always want to update the timestamp):

START u=node:node_auto_index(UserIdentifier = 'USER12'), p=node:node_auto_index(PostIdentifier = 'USER5POST4')
create unique u-[l:LIKE]->p
set l.timestamp = timestamp()

Wes

Aran Mulholland

unread,
Jun 30, 2013, 7:35:33 PM6/30/13
to ne...@googlegroups.com
Thanks Wes, but I don't want to update the time stamp. I only want to create if the relationship does not exist.

Michael Hunger

unread,
Jun 30, 2013, 8:17:43 PM6/30/13
to ne...@googlegroups.com
You can filter with WHERE,

START u=node:node_auto_index(UserIdentifier = 'USER12'), p=node:node_auto_index(PostIdentifier = 'USER5POST4')
create unique u-[l:LIKE]->p
       WHERE not ( has (l.timestamp))
set l.timestamp = timestamp()


If you want to return l you have to do some collection magic:


START u=node:node_auto_index(UserIdentifier = 'USER12'), p=node:node_auto_index(PostIdentifier = 'USER5POST4')
create unique u-[l:LIKE]->p
       WITH collect(l) as rels
       FOREACH (r2 in filter(r1 in rels : not (has (l.timestamp))) r2.timestamp = timestamp())
       RETURN head(rels) as l

If you already use 2.0 you will be able to use MERGE with ON CREATE, but it is not done yet, meaning that the current M03 only supports merge for single node patterns not full patterns.

if you still want to return l, you might need  use use CASE WHEN in 2.0

Michael

Brian Gates

unread,
Jul 15, 2013, 3:56:32 PM7/15/13
to ne...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages