使用Neo4J 2.0.0-M5,我正在尝试使用批处理创建数据库,如下所述: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
我想要做的是使用“get_or_create”模式插入节点和关系,这样我就可以多次运行批处理,而不会创建冗余的节点和关系。
可以使用/index/node/indexname?uniqueness=get_or_create
创建唯一节点,例如:
POST /db/data/batch [
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":0,"body":{"key":"nom", "value":"organisation", "properties": {"nom":"organisation"}}},
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":1,"body":{"key":"nom", "value":"établissement", "properties": {"nom":"établissement"}}}
]
但是,我没有找到如何在两个索引节点之间创建唯一关系?
我想说“établissement”和“组织”之间存在“某种”关系。如果我尝试:
POST /db/data/batch [
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":0,"body":{"key":"nom", "value":"organisation", "properties": {"nom":"organisation"}}},
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":1,"body":{"key":"nom", "value":"établissement", "properties": {"nom":"établissement"}}},
{"method":"POST","to":"{1}/relationships","body":{"to":"{0}","type":"est"}},
{"method":"POST","to":"/index/relationship/my_rels?uniqueness=get_or_create","body":{"key":"nom","value":"est","uri":"{1}"}}
]
我明白了:
==> 500 Internal Server Error
==> {
==> "message" : "",
==> "exception" : "BatchOperationFailedException",
==> "fullname" : "org.neo4j.server.rest.domain.BatchOperationFailedException",
==> "stacktrace" : ["org.neo4j.server.rest.batch.NonStreamingBatchOperations.invoke(NonStreamingBatchOperations.java:63)", "org.neo4j.server.rest.batch.BatchOperations.performRequest(BatchOperations.java:188)", "org.neo4j.server.rest.batch.BatchOperations.parseAndPerform(BatchOperations.java:159)", "org.neo4j.server.rest.batch.NonStreamingBatchOperations.performBatchJobs(NonStreamingBatchOperations.java:48)", "org.neo4j.server.rest.web.BatchOperationService.batchProcess(BatchOperationService.java:123)", "org.neo4j.server.rest.web.BatchOperationService.performBatchOperations(BatchOperationService.java:73)", "java.lang.reflect.Method.invoke(Method.java:606)", "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:132)", "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)" ]
==> }
我坚持这个问题,所以任何建议都会非常感激。谢谢!
格雷
编辑:问题似乎来自一个阻止在批处理中引用唯一节点的错误: https://github.com/neo4j/neo4j/issues/84
答案 0 :(得分:1)
解决方法(如您提到的github错误中所述)是使用Cypher。
使用Batch和Cypher的示例:
[
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":0,"body":{"key":"nom", "value":"organisation", "properties": {"nom":"organisation"}}},
{"method":"POST","to":"/index/node/concept?uniqueness=get_or_create","id":1,"body":{"key":"nom", "value":"établissement", "properties": {"nom":"établissement"}}},
{
"method": "POST",
"to": "/cypher",
"id": 1,
"body": {
"query" : "START a=node:concept(nom={aVal}), b=node:concept(nom={bVal}) CREATE b-[r:est]->a RETURN a, b, r",
"params" : {
"aVal" : "établissement",
"bVal" : "organisation"
}
}
}
]
输出:
[{
"id": 0,
"location": "http://localhost:7474/db/data/index/node/concept/nom/organisation/18688",
"body": {
"extensions": {},
"paged_traverse": "http://localhost:7474/db/data/node/18688/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships": "http://localhost:7474/db/data/node/18688/relationships/out",
"all_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/all/{-list|&|types}",
"traverse": "http://localhost:7474/db/data/node/18688/traverse/{returnType}",
"property": "http://localhost:7474/db/data/node/18688/properties/{key}",
"all_relationships": "http://localhost:7474/db/data/node/18688/relationships/all",
"self": "http://localhost:7474/db/data/node/18688",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/out/{-list|&|types}",
"properties": "http://localhost:7474/db/data/node/18688/properties",
"incoming_relationships": "http://localhost:7474/db/data/node/18688/relationships/in",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/in/{-list|&|types}",
"create_relationship": "http://localhost:7474/db/data/node/18688/relationships",
"data": {
"nom": "organisation"
},
"indexed": "http://localhost:7474/db/data/index/node/concept/nom/organisation/18688"
},
"from": "/index/node/concept?uniqueness=get_or_create"
}, {
"id": 1,
"location": "http://localhost:7474/db/data/index/node/concept/nom/%EF%BF%BDtablissement/18689",
"body": {
"extensions": {},
"paged_traverse": "http://localhost:7474/db/data/node/18689/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships": "http://localhost:7474/db/data/node/18689/relationships/out",
"all_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/all/{-list|&|types}",
"traverse": "http://localhost:7474/db/data/node/18689/traverse/{returnType}",
"property": "http://localhost:7474/db/data/node/18689/properties/{key}",
"all_relationships": "http://localhost:7474/db/data/node/18689/relationships/all",
"self": "http://localhost:7474/db/data/node/18689",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/out/{-list|&|types}",
"properties": "http://localhost:7474/db/data/node/18689/properties",
"incoming_relationships": "http://localhost:7474/db/data/node/18689/relationships/in",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/in/{-list|&|types}",
"create_relationship": "http://localhost:7474/db/data/node/18689/relationships",
"data": {
"nom": "�tablissement"
},
"indexed": "http://localhost:7474/db/data/index/node/concept/nom/%EF%BF%BDtablissement/18689"
},
"from": "/index/node/concept?uniqueness=get_or_create"
}, {
"id": 1,
"body": {
"columns": ["a", "b", "r"],
"data": [
[{
"paged_traverse": "http://localhost:7474/db/data/node/18689/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships": "http://localhost:7474/db/data/node/18689/relationships/out",
"data": {
"nom": "�tablissement"
},
"traverse": "http://localhost:7474/db/data/node/18689/traverse/{returnType}",
"all_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/all/{-list|&|types}",
"self": "http://localhost:7474/db/data/node/18689",
"all_relationships": "http://localhost:7474/db/data/node/18689/relationships/all",
"property": "http://localhost:7474/db/data/node/18689/properties/{key}",
"properties": "http://localhost:7474/db/data/node/18689/properties",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/out/{-list|&|types}",
"incoming_relationships": "http://localhost:7474/db/data/node/18689/relationships/in",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/18689/relationships/in/{-list|&|types}",
"extensions": {},
"create_relationship": "http://localhost:7474/db/data/node/18689/relationships"
}, {
"paged_traverse": "http://localhost:7474/db/data/node/18688/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships": "http://localhost:7474/db/data/node/18688/relationships/out",
"data": {
"nom": "organisation"
},
"traverse": "http://localhost:7474/db/data/node/18688/traverse/{returnType}",
"all_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/all/{-list|&|types}",
"self": "http://localhost:7474/db/data/node/18688",
"all_relationships": "http://localhost:7474/db/data/node/18688/relationships/all",
"property": "http://localhost:7474/db/data/node/18688/properties/{key}",
"properties": "http://localhost:7474/db/data/node/18688/properties",
"outgoing_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/out/{-list|&|types}",
"incoming_relationships": "http://localhost:7474/db/data/node/18688/relationships/in",
"incoming_typed_relationships": "http://localhost:7474/db/data/node/18688/relationships/in/{-list|&|types}",
"extensions": {},
"create_relationship": "http://localhost:7474/db/data/node/18688/relationships"
}, {
"start": "http://localhost:7474/db/data/node/18688",
"data": {},
"self": "http://localhost:7474/db/data/relationship/44187",
"property": "http://localhost:7474/db/data/relationship/44187/properties/{key}",
"properties": "http://localhost:7474/db/data/relationship/44187/properties",
"type": "est",
"extensions": {},
"end": "http://localhost:7474/db/data/node/18689"
}]
]
},
"from": "/cypher"
}]