2013-03-24 25 views
3

使用Neo4j版本1.8.1,我試圖利用「密碼」REST入口點來插入許多關係(查詢必須插入關係,如果需要的話,目標節點)。我通過http://christophewillemsen.com/streemz/8/importing-initial-data-with-the-neo4j-rest-api博客文章發現了這種可能性。通過Rest API在一個密碼查詢中執行多個CREATE UNIQUE

如果我只創建一個關係,但只要我嘗試了幾次就失敗了。

的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 Retunred錯誤請撥打:

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

不知道我的查詢究竟如何在Neo4中轉換j很難找到我必須改變我的查詢。

我認爲Neo4j會做2個查詢,但由於錯誤,它似乎在爲另一個節點端做一種IN語句。

我也嘗試使PARAMS作爲列表,但它沒有奏效。

謝謝您的幫助

回答

5

請務必使用參數暗號查詢所有的時間

使用REST分批操作來執行多個查詢,看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 
    } ] 
+0

它似乎回答我的問題。我會在今天早上嘗試驗證。 – 2013-03-26 06:27:22

+0

沒關係,它允許我以正確的性能正確地打我的數據庫到我的數據庫(1000到2秒到4秒創建unqiue查詢)。 – 2013-03-26 10:44:48

0

我用「的Cypher查詢語言」不是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可以通過這個網址:http://docs.neo4j.org/chunked/1.8/rest-api.html

+0

謝謝,它確實可以是一種解決方案,但它不能解決一種情況,即當我希望CREATE UNIQUE創建兩個關係和結束節點。用你的語法,如果用戶21000002不存在,它將不會被創建。 – 2013-03-24 19:33:52

0

我試圖用穆罕默德·奧斯曼·解決方案並構建一個適合我的查詢,但我在REST API中遇到另一個錯誤。

我嘗試的查詢是:

{"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":{}}

它給我的錯誤是:

{ "message": "string matching regex $' expected but S' found\n\nThink we should have better error message here? Help us by sending this query to [email protected]\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": [...] }

從我瞭解的Cypher是expectinq查詢中的第CREATE UNIQUE後結束。然而Cypher允許我們在一個Cypher中做多個CREATE爲什麼。爲什麼不使用多個CREATE UNIQUE?

+0

你可以在console.neo4j.org重新創建這個文件並提交一個錯誤? – 2013-03-26 12:46:11

+0

嗨,我試圖重現這一點,但似乎我不能在console.neo4j.org做一個CREATE UNIQUE。它說'錯誤:java.lang.RuntimeException:我需要一個事務!' – 2013-03-27 08:28:02

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 
+0

我沒有試過,語法沒問題。然而它並沒有做我所需要的,也就是:我不知道節點是否存在,這就是爲什麼我把它放在開始聲明中。但是我的一個START節點不存在,CREATE UNIQUE不會插入任何東西。即使其中一個CREATE UNIQUE語句是正確的。 – 2013-03-27 08:43:28