如何忽视连接的节点
问题描述:
我有一个节点,一个关系如何忽视连接的节点
class User
include Neo4j::ActiveNode
property :first_name
end
class Connection
include Neo4j::ActiveRel
include Enumable
creates_unique
from_class 'User'
to_class 'User'
type 'connected_to'
property :status, type: Integer, default: 0
end
我想找到这是不符合用户1接通了第二度与用户的距离用户1
User.find(1).query_as(:s)
.match('(s) - [r1 :connected_to] - (mutual_friend)
- [r2 :connected_to] - (friends_of_friend: `User`)')
.match('(s)-[r4:connected_to]-[friends_of_friend]')
.where('r1.status = 1 AND r2.status = 1 AND r4 IS NULL')
.pluck('DISTINCT friends_of_friend.uuid').count
但是这是给我0结果每次我也试过可选的匹配,但它给了一个巨大的数字,任何帮助?
答
MATCH不能用于找到缺少模式,那永远不会返回行。使用OPTIONAL MATCH应该可以工作。
或者,如果where_not()
方法允许模式,可以使用:
.where_not('(s)-[:connected_to]-(friends_of_friend)')
作为备用装置排除关系。
答
InverseFalcon是正确的,但还有许多其他的事情可以做,使之变得简单许多:
class User
include Neo4j::ActiveNode
property :first_name
has_many :both, :connected_users, type: :connected_to, model_class: :User
end
# ActiveRel isn't strictly needed for this,
# though to have the `default` or any other logic it's good to have it
user = User.find(1)
user.as(:user)
.connected_users.rel_where(status: 1)
.connected_users(:friend_of_friend).rel_where(status: 1)
.where_not('(user)-[:connected_to]-(friend_of_friend)')
.count(:distinct)
我认为,这也将工作:
user.as(:user)
.connected_users(:friend_of_friend, nil, rel_length: 2).rel_where(status: 1)
.where_not('(user)-[:connected_to]-(friend_of_friend)')
.count(:distinct)
+0
非常感谢,非常有帮助 –
感谢@布赖恩&inverseFalcon –