Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Node ID's in Neo4j using Python

Tags:

neo4j

cypher

I have recently begun using Neo4j and am struggling to understand how things work. I am trying to create relationships between nodes that I created earlier in my script. The cypher query that I found looks like it should work, but I don't know how to get the id's to replace the #'s

START a= node(#), b= node(#)
CREATE UNIQUE a-[r:POSTED]->b
RETURN r
like image 458
drowningincode Avatar asked Mar 24 '23 12:03

drowningincode


1 Answers

If you want to use plain cypher, the documentation has a lot of usage examples.

When you create nodes you can return them (or just their ids by returning id(a)), like this:

CREATE (a {name:'john doe'}) RETURN a

This way you can keep the id around to add relationships.

If you want to attach relationships later, you should not use the internal id of the nodes to reference them from external system. They can for example be re-used if you delete and create nodes.

You can either search for a node by scanning over all and filtering using WHERE or add an index to your database, e.g. if you add an auto_index on name:

START n = node:node_auto_index(name='john doe') 

and continue from there. Neo4j 2.0 will support index lookup transparently so that MATCH and WHERE should be as efficient.

If you are using python, you can also take a look at py2neo which provides you with a more pythonic interface while using cypher and the REST interface to communicate with the server.

like image 103
Thomas Fenzl Avatar answered May 03 '23 17:05

Thomas Fenzl