Re: [Neo4j] Create node / make sure a node exists

161 views
Skip to first unread message

Peter Neubauer

unread,
May 14, 2013, 8:55:47 AM5/14/13
to Neo4j User
Michael,
in Neo4j 2.0, there are a number of better ways to do this see MERGE
and CREATE UNIQUE,

http://docs.neo4j.org/chunked/preview/query-create-unique.html
http://docs.neo4j.org/chunked/preview/query-merge.html

Is that what you are looking for?

/peter

Cheers,

/peter neubauer

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

The authoritative book on graph databases - http://graphdatabases.com
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j


On Tue, May 14, 2013 at 1:40 PM, Michael B. <wtfstfu...@gmail.com> wrote:
> Hey guys!
>
> Just 5 cents of wisdom I'd like to share with the community:
>
> START x=node:node_auto_index(key="x")
> with count(x) as exists
> start y=node:node_auto_index(key="y")
> where exists = 0
> create (n {key:"y"})<-[:rel]-y
> return n, y
>
>
> It's kind of hard to make sure that nodes exist before performing further
> operations on them. With 2.0 this might be a bit nicer with if-then-else
> statements...
>
> Regards,
> Michael
>
> --
> 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 B.

unread,
May 14, 2013, 10:07:17 AM5/14/13
to ne...@googlegroups.com
Hi Peter,

merge is way more appropirate.

The issue I'm working around here is that for the create unique statement, if the relationship doesn't exist, the second node is always created along. For example:

start n=node(0)
create unique n
-[:r]->(x {a:'b'})

This would create new x node anew. If you want to use an existing node, but only create the relationship r if it doesn't exist yet, you have to query for it separately. This is what my query from the first post does.

Just looking into the options here... what would perform best?

Peter Neubauer

unread,
Jun 13, 2013, 3:15:47 PM6/13/13
to Neo4j User
Michal,
CREATE UNIQUE is going away and replace by MERGE, which supports non-connected nodes, giving you exactly what you want. then, you can even react on the creation or non-creation of the relationship with ON CREATE etc, see http://docs.neo4j.org/chunked/snapshot/query-merge.html#merge-merge-single-node-specifying-both-label-and-property

Does that help?

/peter


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? Please use SO - http://stackoverflow.com/search?q=neo4j

Michael B.

unread,
Jun 13, 2013, 3:40:35 PM6/13/13
to ne...@googlegroups.com, Peter Neubauer
On Donnerstag, 13. Juni 2013 21:15:47, Peter Neubauer wrote:
> Michal,
> CREATE UNIQUE is going away and replace by MERGE, which supports
> non-connected nodes, giving you exactly what you want. then, you can
> even react on the creation or non-creation of the relationship with ON
> CREATE etc, see
> http://docs.neo4j.org/chunked/snapshot/query-merge.html#merge-merge-single-node-specifying-both-label-and-property
>
> Does that help?
>
> /peter
>
>
> 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? Please use SO - http://stackoverflow.com/search?q=neo4j
>
>
> On Tue, May 14, 2013 at 4:07 PM, Michael B. <wtfstfu...@gmail.com
> <mailto:wtfstfu...@gmail.com>> wrote:
>
> Hi Peter,
>
> merge is way more appropirate.
>
> The issue I'm working around here is that for the create unique
> statement, if the relationship doesn't exist, the second node is
> *always *created along. For example:
>
> |
> start n=node(0)
> create unique n-[:r]->(x {a:'b'})
> |
>
> This would create new x node anew. If you want to use an existing
> node, but only create the relationship */r/* if it doesn't exist
> yet, you have to query for it separately. This is what my query
That's nice. I was just looking at this sort of thing today... Needed
to make sure the relationships get a weight property on creation. With
merge and on create, this'll probably be way more convenient.

Although from my p.o.v. this isn't the most pressing issue. The locking
of non-existent nodes in a multi-threaded environment for these
merge-statements should have priority. So far, I've tried a whole bunch
of different approaches for batch loading unique nodes in a
multi-client environment and haven't been able to get good performance.

Regards,
Michael

> from the first post does.
>
> Just looking into the options here... what would perform best?
>
>
> On Tuesday, 14 May 2013 14:55:47 UTC+2, Peter Neubauer wrote:
>
> Michael,
> in Neo4j 2.0, there are a number of better ways to do this see
> MERGE
> and CREATE UNIQUE,
>
> http://docs.neo4j.org/chunked/__preview/query-create-unique.__html
> <http://docs.neo4j.org/chunked/preview/query-create-unique.html>
> http://docs.neo4j.org/chunked/__preview/query-merge.html
> <http://docs.neo4j.org/chunked/preview/query-merge.html>
>
> Is that what you are looking for?
>
> /peter
>
> Cheers,
>
> /peter neubauer
>
> G: neubauer.peter
> S: peter.neubauer
> P: +46 704 106975 <tel:%2B46%20704%20106975>
> L: http://www.linkedin.com/in/__neubauer
> <http://www.linkedin.com/in/neubauer>
> T: @peterneubauer
>
> The authoritative book on graph databases -
> http://graphdatabases.com
> Neo4j questions? Please use SO -
> http://stackoverflow.com/__search?q=neo4j
> <http://stackoverflow.com/search?q=neo4j>
>
>
> On Tue, May 14, 2013 at 1:40 PM, Michael B.
> <wtfstfu...@gmail.com> wrote:
> > Hey guys!
> >
> > Just 5 cents of wisdom I'd like to share with the community:
> >
> > START x=node:node_auto_index(key="x"__)
> > with count(x) as exists
> > start y=node:node_auto_index(key="y"__)
> > where exists = 0
> > create (n {key:"y"})<-[:rel]-y
> > return n, y
> >
> >
> > It's kind of hard to make sure that nodes exist before
> performing further
> > operations on them. With 2.0 this might be a bit nicer with
> if-then-else
> > statements...
> >
> > Regards,
> > Michael
> >
> > --
> > 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
> <https://groups.google.com/groups/opt_out>.
> >
> >
>
> --
> 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
> <mailto:neo4j%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Neo4j" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/neo4j/VN5bR07PkGw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Michael Hunger

unread,
Jun 13, 2013, 6:21:17 PM6/13/13
to ne...@googlegroups.com, ne...@googlegroups.com, Peter Neubauer
You can also lookup/bind the second node

For just checking on the reltype you can use CREATE UNIQUE on the reltype and the SET later on on the rel

Sent from mobile device
> To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages