我有一个节点和一个关系
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
我想从User1找到与User1尚未连接的二级用户
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个结果,但它给出了一个巨大的数字,对此有任何帮助吗?
答案 0 :(得分:2)
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)
答案 1 :(得分:1)
MATCH不能用于查找缺少模式,永远不会返回行。使用OPTIONAL MATCH应该可以工作。
或者,如果where_not()
方法允许使用模式,则可以使用:
.where_not('(s)-[:connected_to]-(friends_of_friend)')
作为排除这种关系的替代方法。