Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node identifiers in neo4j

Tags:

neo4j

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?

like image 839
Jonathan Williamson Avatar asked Jan 29 '12 06:01

Jonathan Williamson


People also ask

What is node label in Neo4j?

Neo4j CQL CREATE a Node Label Label is a name or identifier to a Node or a Relationship in Neo4j Database. We can say this Label name to a Relationship as "Relationship Type". We can use CQL CREATE command to create a single label to a Node or a Relationship and multiple labels to a Node.

How many nodes are in Neo4j?

The limitation is currently 2^35, so approximately 34 billion nodes. See the documentation.

Can a node have more than one label in Neo4j?

Create a node with multiple labels. To add labels when creating a node, use the syntax below. In this case, we add two labels.

What is the syntax for getting all the nodes under specific label in Neo4j?

If you want to get the labels of a specify node, then use labels(node) ; If you only want to get all node labels in neo4j, then use this function instead: call db. labels; , never ever use this query: MATCH n RETURN DISTINCT LABELS(n) . It will do a full table scan, which is very very slow..


1 Answers

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

like image 193
Michael Hunger Avatar answered Sep 24 '22 13:09

Michael Hunger