Get Node ID in Neo4j using Python

I recently started using Neo4j and am struggling to understand how everything works. I am trying to create relationships between nodes that I created earlier in my script. The cypher request I found looks like it should work, but I don't know how to get the identifier to replace #

START a= node(#), b= node(#) CREATE UNIQUE a-[r:POSTED]->b RETURN r 
+4
source share
5 answers

If you want to use simple cypher, the documentation contains many usage examples.

When you create nodes, you can return them (or just their identifiers by returning id(a) ), for example:

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

This way you can save id to add relationships.

If you want to link relationships later, you should not use the internal identifier of nodes to refer to them from an external system. For example, they can be reused if you delete and create nodes.

You can search for a node by scanning all parameters and filtering with WHERE or add index to your database, for example if you add auto_index for the name:

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

and continue from there. Neo4j 2.0 will support index browsing transparently, so MATCH and WHERE should be just as efficient.

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

+5
source

This may be what you are looking for:

 START n = node(*) , x = node(*) Where x<>n CREATE UNIQUE n-[r:POSTED]->x RETURN r 

It will create a POSTED link between all nodes, such as

 +-----------------------+ | r | +-----------------------+ | (0)-[10:POSTED]->(1) | | (0)-[10:POSTED]->(2) | | (0)-[10:POSTED]->(3) | | (1)-[10:POSTED]->(0) | | (1)-[10:POSTED]->(2) | | (1)-[10:POSTED]->(3) | | (2)-[10:POSTED]->(0) | | (2)-[10:POSTED]->(1) | | (2)-[10:POSTED]->(3) | | (3)-[10:POSTED]->(0) | | (3)-[10:POSTED]->(1) | | (3)-[10:POSTED]->(2) | 

And if you do not need the connection between the node (0) link and other nodes, you can make a request as follows

 START n = node(*), x = node(*) WHERE x<>n AND id(n)<>0 AND id(x)<>0 CREATE UNIQUE n-[r:POSTED]->x RETURN r 

and the result will be like this:

 +-----------------------+ | r | +-----------------------+ | (1)-[10:POSTED]->(2) | | (1)-[10:POSTED]->(3) | | (2)-[10:POSTED]->(1) | | (2)-[10:POSTED]->(3) | | (3)-[10:POSTED]->(1) | | (3)-[10:POSTED]->(2) | 
+2
source

On the client side, using Javascript, I send a cypher request:

start n = node(*) WHERE n.name = '" + a.name + "' return n

and then parse the identification number from the "self" response in the form:

server_url:7474/db/data/node/node_id

0
source

A few hours later, trying to figure it out, I finally found what I was looking for. I struggled with the way the nodes returned and found that

 userId=person[0][0][0].id 

will return what I wanted. Thank you for your help!

0
source

Using py2neo, I found it really useful to use a remote module.

 from py2neo import Graph, remote graph = Graph() graph.run('CREATE (a)-[r:POSTED]-(b)') a = graph.run('MATCH (a)-[r]-(b) RETURN a').evaluate() a_id = remote(a)._id b = graph.run('MATCH (a)-[r]-(b) WHERE ID(a) = {num} RETURN b', num=a_id).evaluate() b_id = remote(b)._id graph.run('MATCH (a)-[r]-(b) WHERE ID(a)={num1} AND ID(b)={num2} CREATE (a)-[x:UPDATED]-(b)', num1=a_id, num2=b_id) 

The remote function accepts the py2neo Node object and has the _id attribute, which you can use to return the current identification number from the graph database.

0
source

Source: https://habr.com/ru/post/1486885/


All Articles