【问题标题】:Execute multiple CREATE UNIQUE in one cypher query through Rest API通过 Rest API 在一个密码查询中执行多个 CREATE UNIQUE
【发布时间】:2013-03-24 10:30:48
【问题描述】:

使用 Neo4j 版本 1.8.1,我试图利用“密码”REST 入口点插入许多关系(查询必须插入关系,并且仅在必要时插入目标节点)。我通过http://christophewillemsen.com/streemz/8/importing-initial-data-with-the-neo4j-rest-api 博文发现了这种可能性。

如果我只创建一个关系,它会很好,但我尝试几个时它就会失败。

用于调用一种有效关系的 JSON:

{"query":"START n=node:node_auto_index(UserId='21000001') CREATE UNIQUE n-[:KNOWS{Label:'Friend'}]-(m{Users})", "params":{"Users" : [{"UserId":"21000003"}]}}

我试图建立的 2 关系失败了:

{"query":"START n=node:node_auto_index(UserId='21000001') CREATE UNIQUE n-[:KNOWS{Label:'Friend'}]-(m{Users})", "params":{"Users" : [{"UserId":"21000002"},{"UserId":"21000003"}]}}

REST 调用返回的错误是:

{
    "message": "The pattern CreateUniqueAction(List(m-[:`LOVES`]-n)) produced multiple possible paths, and that is not allowed",
    "exception": "UniquePathNotUniqueException",
    "stacktrace": "..."
}

不知道我的查询在 Neo4j 中是如何转换的,很难找到我必须如何更改我的查询。

我以为 Neo4j 会做 2 次查询,但从错误看来,它似乎在为另一个节点端做某种 IN 语句。

我也尝试将参数设置为列表,但没有成功。

感谢您的帮助

【问题讨论】:

    标签: rest neo4j cypher


    【解决方案1】:

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

    使用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
       } ]
    

    【讨论】:

    • 这似乎回答了我的问题。我会在今天早上尝试验证。
    • 没关系,它允许我以迄今为止最好的性能正确调用我的数据库(1000 在 2 到 4 秒内创建 unqiue 查询)。
    【解决方案2】:

    我曾经使用“密码查询语言”而不是 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

    【讨论】:

    • 谢谢,这确实是一种解决方案,但它不能解决一个场景,即我希望 CREATE UNIQUE 同时创建关系和结束节点。使用您的语法,如果用户 21000002 不存在,则不会创建它。
    【解决方案3】:

    我尝试使用 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", "参数”:{}}

    它给我的错误是:

    { "message": "string matching regex $' expected butS' found\n\n认为我们应该在这里有更好的错误消息?请将此查询发送到 cypher@neo4j.org 帮助我们。\n\n谢谢 Neo4j 团队。\ n\n\"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\"\n ^", “异常”:“语法异常”, “堆栈跟踪”: [...] }

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

    【讨论】:

    • 你能在 console.neo4j.org 中重新创建这个并提交一个错误吗?
    • 嗨,我试图重现这一点,但似乎我也无法在 console.neo4j.org 中执行 CREATE UNIQUE。它说Error: java.lang.RuntimeException: I need a transaction!
    【解决方案4】:

    你试过了吗

    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
    

    【讨论】:

    • 我没试过,语法还可以。然而它并没有做我需要的,那就是:我不知道节点是否存在,这就是为什么我把它放在 start 语句中。但是我的 START 节点之一不存在 CREATE UNIQUE 不会插入任何东西。即使 CREATE UNIQUE 语句之一是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2023-03-18
    • 2018-07-03
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多