Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute multiple CREATE UNIQUE in one cypher query through Rest API

Tags:

rest

neo4j

cypher

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

like image 666
b.moyet Avatar asked Jun 10 '26 21:06

b.moyet


1 Answers

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
   } ]
like image 86
Michael Hunger Avatar answered Jun 12 '26 10:06

Michael Hunger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!