4

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
Blender
  • 289,723
  • 53
  • 439
  • 496
drowningincode
  • 1,115
  • 1
  • 12
  • 19

5 Answers5

5

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.

Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
2

This could 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 POSTED relationship between all the nodes like this

+-----------------------+
| 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 don't want a relation between the reference node(0) and the other nodes, you can make the query like this

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 that:

+-----------------------+
| 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)  |
0

On the client side using Javascript I post the cypher query:

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

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

server_url:7474/db/data/node/node_id

JeffA
  • 166
  • 1
  • 17
0

After hours of trying to figure this out, I finally found what I was looking for. I was struggling with how nodes were getting returned and found that

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

would return what I wanted. Thanks for all your help though!

drowningincode
  • 1,115
  • 1
  • 12
  • 19
0

Using py2neo, the way I've found that is really useful is to use the 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 takes in a py2neo Node object and has an _id attribute that you can use to return the current ID number from the graph database.

baustin
  • 21
  • 3