我是Neo4j的新手 - 昨天晚上才开始玩它.
我注意到所有节点都是由在节点创建期间生成的自动递增的整数来标识的 - 总是这样吗?
我的数据集有自然的字符串键,所以我想避免在Neo4j指定的ID和我自己的ID之间进行映射.是否可以使用字符串标识符?
Mic*_*ger 68
将node-id视为实现细节(如关系数据库的rowid,可用于标识节点,但不应依赖于永远不会重复使用).
您可以将自然键作为属性添加到节点,然后使用自然键索引节点(或为它们启用自动索引).
Java API中的E..g:
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
Run Code Online (Sandbox Code Playgroud)
使用自动索引器,您可以为"id"字段启用自动索引.
// 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();
Run Code Online (Sandbox Code Playgroud)
请参阅:http://docs.neo4j.org/chunked/milestone/auto-indexing.html 并且:http://docs.neo4j.org/chunked/milestone/indexing.html