55
votes

I'm new to Neo4j - just started playing with it yesterday evening.

I've notice all nodes are identified by an auto-incremented integer that is generated during node creation - is this always the case?

My dataset has natural string keys so I'd like to avoid having to map between the Neo4j assigned ids and my own. Is it possible to use string identifiers instead?

5

5 Answers

69
votes

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

0
votes

This should help:

Create the index to back automatic indexing during batch import We know that if auto indexing is enabled in neo4j.properties, each node that is created will be added to an index named node_auto_index. Now, here’s the cool bit. If we add the original manual index (at the time of batch import) and name it as node_auto_index and enable auto indexing in neo4j, then the batch-inserted nodes will appear as if auto-indexed. And from there on each time you create a node, the node will get indexed as well.**

Source : Identifying nodes with Custom Keys

0
votes

According Neo docs there should be automatic indexes in place http://neo4j.com/docs/stable/query-schema-index.html but there's still a lot of limitations

0
votes

Beyond all answers still neo4j creates its own ids to work faster and serve better. Please make sure internal system does not conflict between ids then it will create nodes with same properties and shows in the system as empty nodes.

0
votes

the ID's generated are default and cant be modified by users. user can use your string identifiers as a property for that node.