用户具有称为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
示例
第一种情况: call 者是爱丽丝
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "5678")
OR ((:User { identityId: "5678" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
你是爱丽丝
第二种情况: call 者是鲍勃
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "1234")
OR ((:User { identityId: "1234" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
你是爱丽丝
第三种情况:来电者是马洛里
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "2222")
OR ((:User { identityId: "2222" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
u为NULL(既不是用户,也不是具有Alice的用户已共享其用户)
最佳答案
考虑到您的其他解释:
// 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
关于performance - Neo4j查询:条件匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37520063/