Node identifiers in neo4j

Think of the node-id as an implementation detail (like the rowid of relational databases, can be used to identify nodes but should not be relied on to be never reused).

You would add your natural keys as properties to the node and then index your nodes with the natural key (or enable auto-indexing for them).

E..g in the Java API:

Index<Node> idIndex = db.index().forNodes("identifiers");

Node n = db.createNode();
n.setProperty("id", "my-natural-key");
idIndex.add(n, "id",n.getProperty("id"));

// later
Node n = idIndex.get("id","my-natural-key").getSingle(); // node or null

With auto-indexer you would enable auto-indexing for your “id” field.

// via configuration 
GraphDatabaseService db = new EmbeddedGraphDatabase("path/to/db",
 MapUtils.stringMap( 
    Config.NODE_KEYS_INDEXABLE, "id", Config.NODE_AUTO_INDEXING, "true" ));

// programmatic (not persistent)
db.index().getNodeAutoIndexer().startAutoIndexingProperty( "id" );

// Nodes with property "id" will be automatically indexed at tx-commit
Node n = db.createNode();
n.setProperty("id", "my-natural-key");

// Usage
ReadableIndex<Node> autoIndex = db.index().getNodeAutoIndexer().getAutoIndex();
Node n = autoIndex.get("id","my-natural-key").getSingle();

See: http://docs.neo4j.org/chunked/milestone/auto-indexing.html
And: http://docs.neo4j.org/chunked/milestone/indexing.html

Leave a Comment