Ruby on rails 如何在Neo4j.rb查询中忽略连接节点

Ruby on rails 如何在Neo4j.rb查询中忽略连接节点,ruby-on-rails,neo4j,neo4j.rb,Ruby On Rails,Neo4j,Neo4j.rb,我有一个节点和一个关系 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:

我有一个节点和一个关系

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个结果,但是它给出的结果非常多,关于这个匹配的任何帮助都不能用于查找缺少的模式,它永远不会返回行。使用可选的匹配应该可以

或者,如果
where_not()
方法允许模式,则可以使用:

 .where_not('(s)-[:connected_to]-(friends_of_friend)')

作为排除该关系的另一种方法。

无法使用匹配来查找缺少的模式,该模式将永远不会返回行。使用可选的匹配应该可以

或者,如果
where_not()
方法允许模式,则可以使用:

 .where_not('(s)-[:connected_to]-(friends_of_friend)')

作为排除这种关系的另一种方法。

倒法尔康是正确的,不过您可以做其他各种事情来简化它:

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)

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)

谢谢,太棒了,非常有用了谢谢,太棒了,非常有用了,谢谢@brian&inverseFalconThanks@brian&inverseFalcon