3

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

4

4 回答 4

5

请确保始终在密码查询中使用参数

使用 rest-batch-operations 执行多个查询,请参阅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
   } ]
于 2013-03-25T12:40:05.010 回答
0

我曾经使用“密码查询语言”而不是 REST API。我做你想做的事情:

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

你可以检查这个:http ://docs.neo4j.org/chunked/1.8/cypher-query-lang.html

对于 Rest API,您可以查看以下 URL:http ://docs.neo4j.org/chunked/1.8/rest-api.html

于 2013-03-24T11:03:14.943 回答
0

我尝试使用 Muhammad Osman 解决方案并构建一个适合我的查询,但在 REST API 中遇到另一个错误。

我试过的查询是:

{"query":"START n=node:node_auto_index('UserId:21000001'), m=node:node_auto_index('UserId:21000002') 创建唯一 n-[r:KNOWS{Label:'Friend'}]-m START n1=node:node_auto_index('UserId:21000003'), m1=node:node_auto_index('UserId:21000005') 创建唯一 n1-[r1:KNOWS{Label:'Follow'}]-m1", "params": {}}

它给我的错误是:

{ "message": "找到匹配正则表达式$' expected butS' 的字符串\n\n认为我们应该在这里有更好的错误消息?请将此查询发送到 cypher@neo4j.org 帮助我们。\n\n谢谢 Neo4j 团队。\n\ n\"开始 n=node:node_auto_index('UserId:21000001'), m=node:node_auto_index('UserId:21000002') 创建唯一 n-[r:KNOWS{Label:'Friend'}]-m START n1= node:node_auto_index('UserId:21000003'), m1=node:node_auto_index('UserId:21000005') 创建唯一 n1-[r1:KNOWS{Label:'Follow'}]-m1\"\n ^", "exception ": "语法异常", "stacktrace": [...] }

据我了解,Cypher 期望查询在第一个 CREATE UNIQUE 之后结束。然而,Cypher 允许我们在一个 Cypher 中执行多个 CREATE 为什么。为什么不使用多个 CREATE UNIQUE ?

于 2013-03-25T08:18:37.247 回答
0

你试过这个吗

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
于 2013-03-26T20:16:46.990 回答