Neo4j查询:条件匹配

时间:2016-05-30 07:35:03

标签: performance neo4j cypher

我有这个型号:

enter image description here

  • Bob和Alice是用户
  • CVI是一家诊所
  • 冥王星是一种动物

用户有一个名为 identityId (CONSTRAINT UNIQUE)的属性来识别用户。

我想选择具有给定id的User,只要它是用户本身(相同的identityId),或者它是否存在Alice和Bob之间的SHARED_WITH关系。

就性能而言,查询下面是最佳查询吗?

MATCH (u:User) 
WHERE id(u) = {id} AND ((u.identityId = {identityId}) 
                OR ((:User { identityId: {identityId} }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u))) 
RETURN u

示例

  • Alice {id:6,identityId:&#34; 5678&#34;}
  • Bob {id:3,identityId:&#34; 1234&#34;}
  • Mallory {id:5,identityId:&#34; 2222&#34;}

第一种情况:来电者是Alice

MATCH (u:User) 
WHERE id(u) = 6 AND ((u.identityId = "5678") 
                OR ((:User { identityId: "5678" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u))) 
RETURN u

你是爱丽丝

第二种情况:来电者是鲍勃

MATCH (u:User) 
WHERE id(u) = 6 AND ((u.identityId = "1234") 
                OR ((:User { identityId: "1234" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u))) 
RETURN u

你是爱丽丝

第三种情况:来电者是Mallory

MATCH (u:User) 
WHERE id(u) = 6 AND ((u.identityId = "2222") 
                OR ((:User { identityId: "2222" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u))) 
RETURN u

u为NULL(mallory既不是用户也不是Alice已共享其用户的用户)

1 个答案:

答案 0 :(得分:0)

考虑到您的其他解释:

// Get user by id
MATCH (I:User) WHERE id(I) = {id}
  // Whom with given {identityId} shard with him
  OPTIONAL MATCH (U:User {identityId: {identityId} })
                 -[:OWNS]->()<-[:SHARED_WITH]-
                 (I)
  WITH I, COUNT(U) as UC
    // Test user {identityId} 
    // or there are those who with {identityId} are with him shares
    WHERE I.identityId = {identityId} OR UC > 0
RETURN I