2012-11-09 36 views
0

在尝试使用neo4j继续前进的基础知识之前,我正在努力学习。喜欢查询方面,但现在试图删除使用neo4jclient和卡住。如何使用Neo4jClient删除关系

简单设置
根 - [:has_user] - >用户 和 用户 - [:friends_with] - > friend`

对于具有1的ID的用户我想删除从指定的ID == 2.用户1不再是朋友与用户2 :(

总之,使用neo4jclient我第一次检查,以确保用户的朋友在第一时间与此:

if (client.Cypher.Start("root", client.RootNode) 
    .Match("root-[:HAS_USER]->user-[:FRIEND]->friend") 
    .Where((UserNode user, UserNode friend) => user.Id == 1 && friend.Id == id) 
    .Return<Node<UserNode>>("user") 
    .Results 
    .Count() == 1) 
{ 

现在我试图删除:

client.Cypher.Start("root", client.RootNode) 
     .Match("root-[:HAS_USER]->user-[r]->friend") 
     .Where("user.Id = 1") 
     .And() 
     .Where("friend.Id = " + id) 
     .And() 
     .Where(string.Format("type(r) = 'FRIEND'"))     
     .Delete("r"); 
} 

没有错误,但关系仍然存在。有任何想法吗?

更新2012年11月12日

得到它的工作。我首先通过Neo4J实例更新了稳定的1.8。我认为最新的neo4jclient和neo4j服务器没有一起工作。我首先根据id获得用户节点,然后从该节点测试节点是否存在关系,然后才能将其删除。下面的代码:

 var currentUserNode = client.Cypher.Start("root", client.RootNode) 
      .Match("root-[:HAS_USER]->user") 
      .Where((UserNode user) => user.Id == 1) 
      .Return<Node<UserNode>>("user") 
      .Results.Single(); 

     if (currentUserNode.StartCypher("user") 
       .Match("user-[r]->friend") 
       .Where("friend.Id = " + id).And() 
       .Where("type(r) = 'FRIEND'") 
      .Return<Node<UserNode>>("user") 
      .Results 
      .Count() == 1) 
     { 

      currentUserNode.StartCypher("user") 
       .Match("user-[r]->friend") 
       .Where("friend.Id = " + id).And() 
       .Where("type(r) = 'FRIEND'") 
       .Delete("r").ExecuteWithoutResults(); 
     } 
+1

如果您在控制台中运行纯密码查询它是否工作? – ulkas

+0

好主意,ulkas,我会尝试 – rball

回答

0

一种方法是切换到使用CypherFluentQuery代替:

new CypherFluentQuery(client) 
    .Start("root", client.RootNode) 
    .Match("root-[:HAS_USER]->user-[r]->friend") 
    .Where("user.Val = 1").And() 
    .Where("friend.Val = " + 2).And() 
    .Where("type(r) = 'FRIEND'") 
    .Delete("r").ExecuteWithoutResults(); 

这将做你想做的。

我相信这一切都源自一个bug茎:https://bitbucket.org/Readify/neo4jclient/issue/40/should-be-able-to-add-cypher-delete-clause

至于为什么client.Cypher.Start不工作,因为它应该,我不知道,这个bug是固定的,而应该从1.0.0.479版本工作(撰写本文时为1.0.0.496)

+0

我有最新版本496.试图在ExecuteWithoutResults上添加我现有的代码和CypherFluentQuery,并获取它:执行请求时收到意外的HTTP状态。 查询结果为:START root = node({p0}) MATCH root - [:HAS_USER] - > user- [r] - >朋友 WHERE(user.Id = 1)AND(friend。ID = 2)和(类型(R)= '朋友') DELETEř 响应状态是:400错误的请求 原始响应体是:{ “消息”: “预期return子句\ n \” DELETE r \“\ n ^”, “exception”:“expected return clause \ n \”DELETE r \“\ n ^”, – rball

+0

我正在运行1.6.1,现在更新到1.9.M01:“message” :“未找到密钥:用户”。 – rball

+0

好吧,我在1.8,1.9是非生产版本,当我切换到1.9.M01分贝,我也得到了错误。 据我所知 - neo4jclient目前支持1.8 ...这不是一个很好的答案,但我认为与1.8一起会给你最好的镜头... –