Create entities if MATCH/WHERE is fulfilled

40 views
Skip to first unread message

Johnny Luu

unread,
May 28, 2012, 5:10:50 PM5/28/12
to Neo4j Google Groups
I have a user graph where different users have different read and write access.

If a user wants to create a new group I need to first look up if he is connected to the "Group" access node. If so, he will be able to create a new group node, else, he won't be able to do that.

Is it possible to create a new node only if a MATCH/WHERE is fulfilled (that is he is actually connected to the "Group" access node) or do I have to make two queries (one for checking and one for creating) and manage the whole IF logic in my application app?

Johnny

Sent from my iPad

Peter Neubauer

unread,
May 28, 2012, 7:07:15 PM5/28/12
to ne...@googlegroups.com
Johnnym
I guess you can do that with a with clause, see this snippet to insert
a value into a linked list:

CREATE zero={name:0,value:0}, two={value:2,name:2},
zero-[:LINK]->two-[:LINK]->zero
==== zero ====
MATCH zero-[:LINK*0..]->before,
after-[:LINK*0..]->zero,
before-[old:LINK]->after
WHERE before.value? <= 1 AND
1 <= after.value?
CREATE newValue={name:1,value : 1},
before-[:LINK]->newValue,
newValue-[:LINK]->after
DELETE old

Cheers,

/peter neubauer

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

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Johnny Weng Luu

unread,
May 29, 2012, 3:19:44 AM5/29/12
to ne...@googlegroups.com
That one was hard to get :)

Is an IF clause something that would be a good feature in Cypher you think so you could so something like:

START user = node(111), organization = node(112)
IF (user)-[:HAS_ACCESS]-(access {name: "Group"})
RELATE (organization)-[:HAS_GROUP]->(group {name: {name}})

Johnny

Andres Taylor

unread,
May 29, 2012, 3:59:56 AM5/29/12
to ne...@googlegroups.com
It was also uneccesarily complex.

The CREATE clause will run once per matching sub graph coming from the START/MATCH/WHERE before it. No matches - no create. 100 matches - 100 new nodes.

Check out this example.

Makes sense?

Andrés

Johnny Luu

unread,
May 29, 2012, 4:15:05 AM5/29/12
to ne...@googlegroups.com
Never knew about that.

So simple and powerful!!!

Thanks :D

Johnny

Sent from my iPad

Johnny Luu

unread,
May 29, 2012, 4:15:45 AM5/29/12
to ne...@googlegroups.com
And man, i really love the console.neo4j.org.

It's so simple and amazing to test things out now ^_^

Johnny

Peter Neubauer

unread,
May 29, 2012, 5:07:58 AM5/29/12
to ne...@googlegroups.com
Yeah,
that beast is starting to be really useful, especially with that
live-db endpoint Michael has been adding last night. Very cool!

Cheers,

/peter neubauer

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

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Johnny Luu

unread,
May 29, 2012, 5:54:03 AM5/29/12
to ne...@googlegroups.com
What's live db about?

Johnny

Sent from my iPad

Peter Neubauer

unread,
May 30, 2012, 2:39:28 AM5/30/12
to ne...@googlegroups.com
Oh,
it's for testing out stuff, and about an easy way to share setups and
problem descriptions. Nothing for production I fear but highly usable
even in scenarios like the manual, click on the "try this query live"
buttons in http://docs.neo4j.org/chunked/snapshot/query-where.html for
instance.

WDYT?

Cheers,

/peter neubauer

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

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Michael Hunger

unread,
May 30, 2012, 2:56:16 AM5/30/12
to ne...@googlegroups.com
What Peter means is that you can import your running, persistent neo4j db (only small ones) by using

http://console.neo4j.org?init=http://host:port/db/data/cypher

Michael

Johnny Luu

unread,
May 30, 2012, 4:12:04 PM5/30/12
to ne...@googlegroups.com
I just tried it and I have to say I love working with my dataset on the console.neo4j.org more than on data browser.

Hope it will be integrated soon into web admin so you can share your graph with others for collaboration and error reports.

Johnny

Sent from my iPad

Michael Hunger

unread,
May 30, 2012, 5:10:18 PM5/30/12
to ne...@googlegroups.com
Thanks for the feedback and all your great questions and suggestions. Really appreciated.

You can already share your graph on the console with one click as an URL.

It can also generate mutating cypher that you just have to paste into webadmin to re-create the graph in your persistent database.

And it can already import databases (as outlined below) from a running web-admin (actually by just executing a cypher statement and pulling all (or selected) nodes and rels).

I'm not really sure what else you're looking for?

Cheers

Michael

Peter Neubauer

unread,
May 30, 2012, 7:29:57 PM5/30/12
to ne...@googlegroups.com

Johnny,
If you feel like it, doing a short screencast ob how to use the console might be a great idea to shoe others? Loved your Node.js one :-)

Send from mobile.

Gabriel Matei Toma

unread,
Jun 2, 2012, 1:25:46 AM6/2/12
to ne...@googlegroups.com
Hey,
For a link like "http://console.neo4j.org/?init=http://127.0.0.1:7474/db/data/cypher", result is "Connection refused".
What/where do i need to setup to make it work?  

Thanks,
Gabi

Michael Hunger

unread,
Jun 2, 2012, 3:23:19 AM6/2/12
to ne...@googlegroups.com
Gabi,

it can only access publicly accessible IP's and your localhost isn't one of them :)

If you have an external IP and an open port (port forwarding) to your local machine then it works.

Alternatively you can just download rabbithole from the github project http://github.com/neo4j-contrib/rabbithole

and build and run it locally:
mvn package exec:java

it is then available at http://localhost:8080

so you can import your dataset with


Cheers

Michael

Gabriel Matei Toma

unread,
Jun 2, 2012, 4:48:39 AM6/2/12
to ne...@googlegroups.com
Thanks a lot, works like a charm!

I don't know why i thought it was more of a browser app...capable of accessing local data

Michael Hunger

unread,
Jun 2, 2012, 4:50:40 AM6/2/12
to ne...@googlegroups.com
Hmm, 

I'm not sure how far browser (javascript) apps can access local data. But actually the import is done on the server side anyway and that's out there at heroku :)

Cheers

Michael

Gabriel Matei Toma

unread,
Jun 2, 2012, 4:53:42 AM6/2/12
to ne...@googlegroups.com
Hmmm....is it possible to use local storage ? 

Michael Hunger

unread,
Jun 2, 2012, 5:14:27 AM6/2/12
to ne...@googlegroups.com
Most of the app is in server side java code as it uses a real Neo4j instance behind the scenes running cypher and other on the stored graph.
There's only a little javascript for the UI and rendering.

I assume you meant browser local storage?

Michael

Johnny Luu

unread,
Jun 5, 2012, 1:07:47 PM6/5/12
to ne...@googlegroups.com
I could absolutely do that.

I'll contact you in Skype and we can talk about it.

Johnny

Sent from my iPad

Johnny Luu

unread,
Jun 5, 2012, 1:11:50 PM6/5/12
to ne...@googlegroups.com
I feel that they are separated from each other and people don't know how to use them together. Eg there is no link from the web admin to fire up the database in the console.neo4j.org.

And what is the query for importing our database?

Johnny

Sent from my iPad

Peter Neubauer

unread,
Jun 11, 2012, 7:33:19 AM6/11/12
to ne...@googlegroups.com
Johnny,
I think the cypher query is trying to import everything form your
cypher endpoint, something like

start n = node(*) match n-[r]-() return n, r


We maybe should make that configurable, essentially turning Heroku and
console.neo4j.org into a graph view tool much like Gephi or QlikView
with cutom Cypher import?

Cheers,

/peter neubauer

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

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Reply all
Reply to author
Forward
0 new messages