Tony Ennis
Tony Ennis

Reputation: 12289

Fast access to nodes in Neo4j

I have a method that creates a graph with nodes and relationships yadda. I want to return a list of node IDs to my caller so a later step can quickly locate these nodes and create additional relationships. I won't be able to return the Node instances.

In the SQL world, I could return a primary or unique key. In the neo4j world, what do I return? Is there a unique node identifier I can use without dooming myself to the 7th level of Hell? Do I make my own ID? Since I am steeped in SQL, I am having trouble learning the Neo4J way.

Upvotes: 2

Views: 111

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39905

Each node (and relationship) in Neo4j has an ID which technically is a offset pointer in the respective datastore file. You need to be aware that deleting some node and creating new nodes might result in the new nodes having the ids from the previously used nodes (since they reuse the free areas in the file).

In case you are sure that you do not delete anything in your graph you can safely use the internal node id, e.g. via MATCH (n) RETURN id(n) limit 5. If you do delete stuff in your graph using the node ids is a way to 7th level of hell :-)

A more safe approach would be to assign the nodes to be referenced an artificial identifier, e.g. a uuid, put that in the index. There is some tooling available for this, e.g. https://github.com/graphaware/neo4j-uuid

Upvotes: 2

Related Questions