问题
Using Neo4j version 1.8.1, I'm trying to make use of the "cypher" REST entry point to insert many relationship (the query must insert the relationship, and only if necessary the destination node). I found this possibility through http://christophewillemsen.com/streemz/8/importing-initial-data-with-the-neo4j-rest-api blog post.
It works well if I create only one relationship but it fails as soon as I try several.
The JSON used to make the call for one relationship which is working :
{"query":"START n=node:node_auto_index(UserId='21000001') CREATE UNIQUE n-[:KNOWS{Label:'Friend'}]-(m{Users})", "params":{"Users" : [{"UserId":"21000003"}]}}
The one I tried to make 2 relationship which is failing :
{"query":"START n=node:node_auto_index(UserId='21000001') CREATE UNIQUE n-[:KNOWS{Label:'Friend'}]-(m{Users})", "params":{"Users" : [{"UserId":"21000002"},{"UserId":"21000003"}]}}
The error Retunred by the REST call is :
{
"message": "The pattern CreateUniqueAction(List(m-[:`LOVES`]-n)) produced multiple possible paths, and that is not allowed",
"exception": "UniquePathNotUniqueException",
"stacktrace": "..."
}
Not knowing how exactly my query is transformed inside Neo4j it's hard to find how I must change my query.
I thought that Neo4j would do 2 queries, but by the errors it seems that it is doing kind of IN statement for the other node end.
I also tried to make params as a list but it did not worked.
Thank you for your help
回答1:
please make sure to use parameters in cypher queries all the time
Use the rest-batch-operations to execute multiple queries, see http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
POST `http://localhost:7474/db/data/batch`
Accept: application/json
Content-Type: application/json
[ {
"method" : "POST",
"to" : "/cypher",
"body" : {
"query" : "START n=node:node_auto_index(UserId={userId1}), m=node:node_auto_index(UserId={userId2}) CREATE UNIQUE n-[r:KNOWS{Label:{label}}]-m",
"params" : {"userId1":"21000001", "userId2":"21000002","label":"Friend"}
},
"id" : 0
},
{
"method" : "POST",
"to" : "/cypher",
"body" : {
"query" : "START n=node:node_auto_index(UserId={userId1}), m=node:node_auto_index(UserId={userId2}) CREATE UNIQUE n-[r:KNOWS{Label:{label}}]-m",
"params" : {"userId1":"21000003", "userId2":"21000005","label":"Friend"}
},
"id" : 1
} ]
回答2:
I used to use "Cypher Query Language" not REST API. and I do what you want to do like this:
START n=node:node_auto_index('UserId:21000001'),m=node:node_auto_index('UserId:21000002 OR UserId:21000003')
CREATE UNIQUE n-[r:KNOWS{Label:'Friend'}]-m
RETURN r
you can check this: http://docs.neo4j.org/chunked/1.8/cypher-query-lang.html
for Rest API you can check this URL: http://docs.neo4j.org/chunked/1.8/rest-api.html
回答3:
I tried to use Muhammad Osman solution and to build one query that fits me but I encounter another error in the REST API.
The query I tried was :
{"query":"START n=node:node_auto_index('UserId:21000001'), m=node:node_auto_index('UserId:21000002') CREATE UNIQUE n-[r:KNOWS{Label:'Friend'}]-m START n1=node:node_auto_index('UserId:21000003'), m1=node:node_auto_index('UserId:21000005') CREATE UNIQUE n1-[r1:KNOWS{Label:'Follow'}]-m1", "params":{}}
The error it gave me was :
{ "message": "string matching regex
$' expected but
S' found\n\nThink we should have better error message here? Help us by sending this query to cypher@neo4j.org.\n\nThank you, the Neo4j Team.\n\n\"START n=node:node_auto_index('UserId:21000001'), m=node:node_auto_index('UserId:21000002') CREATE UNIQUE n-[r:KNOWS{Label:'Friend'}]-m START n1=node:node_auto_index('UserId:21000003'), m1=node:node_auto_index('UserId:21000005') CREATE UNIQUE n1-[r1:KNOWS{Label:'Follow'}]-m1\"\n ^", "exception": "SyntaxException", "stacktrace": [...] }
From what I understand Cypher is expectinq the query to end after the first CREATE UNIQUE. However Cypher allows us to do multiple CREATE in one Cypher why. Why not multiple CREATE UNIQUE ?
回答4:
did you tried this
START n=node:node_auto_index('UserId:21000001'),m=node:node_auto_index('UserId:21000002'),
n1=node:node_auto_index('UserId:21000003'),m1=node:node_auto_index('UserId:21000005')
CREATE UNIQUE n-[r:KNOWS{Label:'Friend'}]-m ,n1-[r1:KNOWS{Label:'Follow'}]-m1
来源:https://stackoverflow.com/questions/15597377/execute-multiple-create-unique-in-one-cypher-query-through-rest-api