我想使用Java API创建一个独特的关系(来自我能够创建唯一的Cypher查询)。
例如:
START n=node:node_auto_index(name='Neo'),
t=node:node_auto_index(name='The Architect')
CREATE UNIQUE n-[r:SPEAKS_WITH]-t
RETURN n AS Neo,r
提前致谢!
答案 0 :(得分:3)
java API是较低级别的。据我所知,没有方便的方法,所以你需要自己实际编码。未经测试,但你可能会得到这个想法:
Transaction transaction = graphDb.beginTx();
try {
ReadableIndex<Node> autoNodeIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex();
Node n = autoNodeIndex.get("name", "Neo").getSingle();
Node a = autoNodeIndex.get("name", "The Architect").getSingle();
tx.acquireWriteLock(n);
tx.acquireWriteLock(a);
Boolean created = false;
for(Relationship r : n.getRelationships(MyRels.SPEAKS_WITH) {
if(r.getOtherNode(n).equals(a)) { // put other conditions here, if needed
created = true;
break;
}
}
if(!created) {
n.createRelationshipTo(a, MyRels.SPEAKS_WITH);
}
tx.success();
} finally {
tx.finish();
}
针对Mattias的评论进行了更新